aiqtech commited on
Commit
3f160bb
Β·
verified Β·
1 Parent(s): 87dacef

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -219
app.py DELETED
@@ -1,219 +0,0 @@
1
- import os
2
- from typing import List
3
- from chainlit.types import AskFileResponse
4
- from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader, PDFLoader
5
- from aimakerspace.openai_utils.prompts import (
6
- UserRolePrompt,
7
- SystemRolePrompt,
8
- AssistantRolePrompt,
9
- )
10
- from aimakerspace.openai_utils.embedding import EmbeddingModel
11
- from aimakerspace.vectordatabase import VectorDatabase
12
- from aimakerspace.openai_utils.chatmodel import ChatOpenAI
13
- import chainlit as cl
14
- from chainlit import user_session
15
- from chainlit.element import Text
16
-
17
- system_template = """\
18
- Use the following context to answer a users question. If you cannot find the answer in the context, say you don't know the answer."""
19
- system_role_prompt = SystemRolePrompt(system_template)
20
-
21
- user_prompt_template = """\
22
- Context:
23
- {context}
24
-
25
- Question:
26
- {question}
27
- """
28
- user_role_prompt = UserRolePrompt(user_prompt_template)
29
-
30
- @cl.on_chat_start
31
- async def init_sidebar():
32
- # μ‚¬μ΄λ“œλ°” 헀더 κΎΈλ―ΈκΈ°
33
- await cl.Sidebar(
34
- cl.Text(content="πŸ“ **파일 μ—…λ‘œλ“œ μ„Ήμ…˜**", style="heading3"),
35
- cl.FilePicker(
36
- accept=[".pdf", ".txt"],
37
- max_size_mb=2,
38
- on_upload=handle_upload,
39
- label="πŸ“€ PDF/TXT μ—…λ‘œλ“œ",
40
- description="μ΅œλŒ€ 2MB 파일만 μ—…λ‘œλ“œ κ°€λŠ₯ν•©λ‹ˆλ‹€"
41
- ),
42
- cl.Separator(),
43
- cl.Text(content="πŸ” **λ¬Έμ„œ 뢄석 μƒνƒœ**", style="heading4"),
44
- cl.ProgressRing(id="progress", visible=False),
45
- cl.Text(id="status", content="λŒ€κΈ° 쀑...", style="caption"),
46
- title="πŸ“š λ¬Έμ„œ 질의 μ‹œμŠ€ν…œ",
47
- persistent=True # πŸ‘ˆ μ‚¬μ΄λ“œλ°” κ³ μ • μ„€μ •
48
- ).send()
49
-
50
-
51
- async def handle_upload(file: AskFileResponse):
52
- # μ§„ν–‰ μƒνƒœ μ—…λ°μ΄νŠΈ
53
- status = user_session.get("status")
54
- progress = user_session.get("progress")
55
-
56
- await status.update(content=f"πŸ” {file.name} 뢄석 쀑...")
57
- await progress.update(visible=True)
58
-
59
- try:
60
- # 파일 처리 둜직
61
- texts = process_file(file)
62
-
63
- # 벑터 DB ꡬ좕
64
- vector_db = VectorDatabase()
65
- vector_db = await vector_db.abuild_from_list(texts)
66
-
67
- # μ„Έμ…˜μ— μ €μž₯
68
- user_session.set("vector_db", vector_db)
69
-
70
- # μƒνƒœ μ—…λ°μ΄νŠΈ
71
- await status.update(content=f"βœ… {len(texts)}개 청크 처리 μ™„λ£Œ!")
72
- await progress.update(visible=False)
73
-
74
- # 파일 정보 μš”μ•½ ν‘œμ‹œ
75
- await cl.Accordion(
76
- title="πŸ“„ μ—…λ‘œλ“œ λ¬Έμ„œ 정보",
77
- content=[
78
- cl.Text(f"파일λͺ…: {file.name}"),
79
- cl.Text(f"크기: {file.size/1024:.1f}KB"),
80
- cl.Text(f"뢄석 μ‹œκ°„: {datetime.now().strftime('%H:%M:%S')}")
81
- ],
82
- expanded=False
83
- ).send()
84
-
85
- except Exception as e:
86
- await cl.Error(
87
- title="파일 처리 였λ₯˜",
88
- content=f"{str(e)}"
89
- ).send()
90
-
91
- class RetrievalAugmentedQAPipeline:
92
- def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None:
93
- self.llm = llm
94
- self.vector_db_retriever = vector_db_retriever
95
-
96
- async def arun_pipeline(self, user_query: str):
97
- context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
98
-
99
- context_prompt = ""
100
- for context in context_list:
101
- context_prompt += context[0] + "\n"
102
-
103
- formatted_system_prompt = system_role_prompt.create_message()
104
-
105
- formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)
106
-
107
- async def generate_response():
108
- async for chunk in self.llm.astream([formatted_system_prompt, formatted_user_prompt]):
109
- yield chunk
110
-
111
- return {"response": generate_response(), "context": context_list}
112
-
113
- text_splitter = CharacterTextSplitter()
114
-
115
-
116
- def process_file(file: AskFileResponse):
117
- import tempfile
118
- import shutil
119
-
120
- print(f"Processing file: {file.name}")
121
-
122
- # Create a temporary file with the correct extension
123
- suffix = f".{file.name.split('.')[-1]}"
124
- with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
125
- # Copy the uploaded file content to the temporary file
126
- shutil.copyfile(file.path, temp_file.name)
127
- print(f"Created temporary file at: {temp_file.name}")
128
-
129
- # Create appropriate loader
130
- if file.name.lower().endswith('.pdf'):
131
- loader = PDFLoader(temp_file.name)
132
- else:
133
- loader = TextFileLoader(temp_file.name)
134
-
135
- try:
136
- # Load and process the documents
137
- documents = loader.load_documents()
138
- texts = text_splitter.split_texts(documents)
139
- return texts
140
- finally:
141
- # Clean up the temporary file
142
- try:
143
- os.unlink(temp_file.name)
144
- except Exception as e:
145
- print(f"Error cleaning up temporary file: {e}")
146
-
147
-
148
- @cl.on_chat_start
149
- async def on_chat_start():
150
- files = None
151
-
152
- # Wait for the user to upload a file
153
- while files == None:
154
- files = await cl.AskFileMessage(
155
- content="Please upload a Text or PDF file to begin!",
156
- accept=["text/plain", "application/pdf"],
157
- max_size_mb=2,
158
- timeout=180,
159
- ).send()
160
-
161
- file = files[0]
162
-
163
- msg = cl.Message(
164
- content=f"Processing `{file.name}`..."
165
- )
166
- await msg.send()
167
-
168
- # load the file
169
- texts = process_file(file)
170
-
171
- print(f"Processing {len(texts)} text chunks")
172
-
173
- # Create a dict vector store
174
- vector_db = VectorDatabase()
175
- vector_db = await vector_db.abuild_from_list(texts)
176
-
177
- chat_openai = ChatOpenAI()
178
-
179
- # Create a chain
180
- retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
181
- vector_db_retriever=vector_db,
182
- llm=chat_openai
183
- )
184
-
185
- # Let the user know that the system is ready
186
- msg.content = f"Processing `{file.name}` done. You can now ask questions!"
187
- await msg.update()
188
-
189
- cl.user_session.set("chain", retrieval_augmented_qa_pipeline)
190
-
191
-
192
- @cl.on_message
193
- async def main(message):
194
- chain = cl.user_session.get("chain")
195
-
196
-
197
-
198
-
199
- # 응닡 μŠ€νƒ€μΌ κ°œμ„ 
200
- msg = cl.Message(
201
- content="",
202
- actions=[
203
- cl.Action(name="source", value="πŸ“‘ μ†ŒμŠ€ 보기"),
204
- cl.Action(name="feedback", value="πŸ’¬ ν”Όλ“œλ°± 남기기")
205
- ]
206
- )
207
-
208
- async for token in result["response"]:
209
- await msg.stream_token(token, is_final=False)
210
-
211
- # μ΅œμ’… λ©”μ‹œμ§€ ν¬λ§·νŒ…
212
- final_content = f"""
213
- 🧠 **AI 뢄석 κ²°κ³Ό**
214
- {msg.content}
215
-
216
- πŸ“Œ μ°Έμ‘° λ¬Έμž₯:
217
- {chr(10).join([f'- {ctx[0][:50]}...' for ctx in result['context']])}
218
- """
219
- await msg.update(content=final_content)