Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,30 @@
|
|
| 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 |
-
|
|
|
|
| 11 |
from aimakerspace.vectordatabase import VectorDatabase
|
| 12 |
-
from
|
| 13 |
import chainlit as cl
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
| 18 |
|
| 19 |
-
system_template = """
|
| 20 |
-
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."""
|
| 21 |
system_role_prompt = SystemRolePrompt(system_template)
|
| 22 |
|
| 23 |
-
user_prompt_template = """\
|
| 24 |
-
Context:
|
| 25 |
-
{context}
|
| 26 |
-
|
| 27 |
-
Question:
|
| 28 |
-
{question}
|
| 29 |
-
"""
|
| 30 |
user_role_prompt = UserRolePrompt(user_prompt_template)
|
| 31 |
|
| 32 |
class RetrievalAugmentedQAPipeline:
|
| 33 |
-
def __init__(self, llm:
|
| 34 |
self.llm = llm
|
| 35 |
self.vector_db_retriever = vector_db_retriever
|
| 36 |
|
|
@@ -42,7 +36,6 @@ class RetrievalAugmentedQAPipeline:
|
|
| 42 |
context_prompt += context[0] + "\n"
|
| 43 |
|
| 44 |
formatted_system_prompt = system_role_prompt.create_message()
|
| 45 |
-
|
| 46 |
formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)
|
| 47 |
|
| 48 |
async def generate_response():
|
|
@@ -53,45 +46,36 @@ class RetrievalAugmentedQAPipeline:
|
|
| 53 |
|
| 54 |
text_splitter = CharacterTextSplitter()
|
| 55 |
|
| 56 |
-
|
| 57 |
def process_file(file: AskFileResponse):
|
| 58 |
import tempfile
|
| 59 |
import shutil
|
| 60 |
-
|
| 61 |
print(f"Processing file: {file.name}")
|
| 62 |
-
|
| 63 |
-
# Create a temporary file with the correct extension
|
| 64 |
suffix = f".{file.name.split('.')[-1]}"
|
| 65 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
| 66 |
-
# Copy the uploaded file content to the temporary file
|
| 67 |
shutil.copyfile(file.path, temp_file.name)
|
| 68 |
print(f"Created temporary file at: {temp_file.name}")
|
| 69 |
-
|
| 70 |
-
# Create appropriate loader
|
| 71 |
if file.name.lower().endswith('.pdf'):
|
| 72 |
loader = PDFLoader(temp_file.name)
|
| 73 |
else:
|
| 74 |
loader = TextFileLoader(temp_file.name)
|
| 75 |
-
|
| 76 |
try:
|
| 77 |
-
# Load and process the documents
|
| 78 |
documents = loader.load_documents()
|
| 79 |
texts = text_splitter.split_texts(documents)
|
| 80 |
return texts
|
| 81 |
finally:
|
| 82 |
-
# Clean up the temporary file
|
| 83 |
try:
|
| 84 |
os.unlink(temp_file.name)
|
| 85 |
except Exception as e:
|
| 86 |
print(f"Error cleaning up temporary file: {e}")
|
| 87 |
|
| 88 |
-
|
| 89 |
@cl.on_chat_start
|
| 90 |
async def on_chat_start():
|
| 91 |
files = None
|
| 92 |
-
|
| 93 |
-
# Wait for the user to upload a file
|
| 94 |
-
while files == None:
|
| 95 |
files = await cl.AskFileMessage(
|
| 96 |
content="Please upload a Text or PDF file to begin!",
|
| 97 |
accept=["text/plain", "application/pdf"],
|
|
@@ -101,36 +85,27 @@ async def on_chat_start():
|
|
| 101 |
|
| 102 |
file = files[0]
|
| 103 |
|
| 104 |
-
msg = cl.Message(
|
| 105 |
-
content=f"Processing `{file.name}`..."
|
| 106 |
-
)
|
| 107 |
await msg.send()
|
| 108 |
|
| 109 |
-
# load the file
|
| 110 |
texts = process_file(file)
|
| 111 |
-
|
| 112 |
print(f"Processing {len(texts)} text chunks")
|
| 113 |
|
| 114 |
-
# Create a dict vector store
|
| 115 |
vector_db = VectorDatabase()
|
| 116 |
vector_db = await vector_db.abuild_from_list(texts)
|
| 117 |
-
|
| 118 |
-
# Initialize ChatOpenAI (API key will be automatically picked up from environment variable)
|
| 119 |
-
chat_openai = ChatOpenAI()
|
| 120 |
|
| 121 |
-
|
|
|
|
| 122 |
retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
|
| 123 |
vector_db_retriever=vector_db,
|
| 124 |
-
llm=
|
| 125 |
)
|
| 126 |
-
|
| 127 |
-
# Let the user know that the system is ready
|
| 128 |
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
|
| 129 |
await msg.update()
|
| 130 |
|
| 131 |
cl.user_session.set("chain", retrieval_augmented_qa_pipeline)
|
| 132 |
|
| 133 |
-
|
| 134 |
@cl.on_message
|
| 135 |
async def main(message):
|
| 136 |
chain = cl.user_session.get("chain")
|
|
@@ -141,4 +116,4 @@ async def main(message):
|
|
| 141 |
async for stream_resp in result["response"]:
|
| 142 |
await msg.stream_token(stream_resp)
|
| 143 |
|
| 144 |
-
await msg.send()
|
|
|
|
| 1 |
import os
|
| 2 |
from typing import List
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
from chainlit.types import AskFileResponse
|
| 6 |
from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader, PDFLoader
|
| 7 |
from aimakerspace.openai_utils.prompts import (
|
| 8 |
UserRolePrompt,
|
| 9 |
SystemRolePrompt,
|
|
|
|
| 10 |
)
|
| 11 |
+
|
| 12 |
+
from aimakerspace.embedding import EmbeddingModel
|
| 13 |
from aimakerspace.vectordatabase import VectorDatabase
|
| 14 |
+
from huggingface_utils.chatmodel import HuggingFaceLLM
|
| 15 |
import chainlit as cl
|
| 16 |
|
| 17 |
+
# Загрузка токена из .env
|
| 18 |
+
load_dotenv()
|
|
|
|
| 19 |
|
| 20 |
+
system_template = """Use the following context to answer a user's question. If you cannot find the answer in the context, say you don't know."""
|
|
|
|
| 21 |
system_role_prompt = SystemRolePrompt(system_template)
|
| 22 |
|
| 23 |
+
user_prompt_template = """Context:\n{context}\n\nQuestion:\n{question}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
user_role_prompt = UserRolePrompt(user_prompt_template)
|
| 25 |
|
| 26 |
class RetrievalAugmentedQAPipeline:
|
| 27 |
+
def __init__(self, llm: HuggingFaceLLM, vector_db_retriever: VectorDatabase) -> None:
|
| 28 |
self.llm = llm
|
| 29 |
self.vector_db_retriever = vector_db_retriever
|
| 30 |
|
|
|
|
| 36 |
context_prompt += context[0] + "\n"
|
| 37 |
|
| 38 |
formatted_system_prompt = system_role_prompt.create_message()
|
|
|
|
| 39 |
formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)
|
| 40 |
|
| 41 |
async def generate_response():
|
|
|
|
| 46 |
|
| 47 |
text_splitter = CharacterTextSplitter()
|
| 48 |
|
|
|
|
| 49 |
def process_file(file: AskFileResponse):
|
| 50 |
import tempfile
|
| 51 |
import shutil
|
| 52 |
+
|
| 53 |
print(f"Processing file: {file.name}")
|
| 54 |
+
|
|
|
|
| 55 |
suffix = f".{file.name.split('.')[-1]}"
|
| 56 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
|
|
|
| 57 |
shutil.copyfile(file.path, temp_file.name)
|
| 58 |
print(f"Created temporary file at: {temp_file.name}")
|
| 59 |
+
|
|
|
|
| 60 |
if file.name.lower().endswith('.pdf'):
|
| 61 |
loader = PDFLoader(temp_file.name)
|
| 62 |
else:
|
| 63 |
loader = TextFileLoader(temp_file.name)
|
| 64 |
+
|
| 65 |
try:
|
|
|
|
| 66 |
documents = loader.load_documents()
|
| 67 |
texts = text_splitter.split_texts(documents)
|
| 68 |
return texts
|
| 69 |
finally:
|
|
|
|
| 70 |
try:
|
| 71 |
os.unlink(temp_file.name)
|
| 72 |
except Exception as e:
|
| 73 |
print(f"Error cleaning up temporary file: {e}")
|
| 74 |
|
|
|
|
| 75 |
@cl.on_chat_start
|
| 76 |
async def on_chat_start():
|
| 77 |
files = None
|
| 78 |
+
while files is None:
|
|
|
|
|
|
|
| 79 |
files = await cl.AskFileMessage(
|
| 80 |
content="Please upload a Text or PDF file to begin!",
|
| 81 |
accept=["text/plain", "application/pdf"],
|
|
|
|
| 85 |
|
| 86 |
file = files[0]
|
| 87 |
|
| 88 |
+
msg = cl.Message(content=f"Processing `{file.name}`...")
|
|
|
|
|
|
|
| 89 |
await msg.send()
|
| 90 |
|
|
|
|
| 91 |
texts = process_file(file)
|
|
|
|
| 92 |
print(f"Processing {len(texts)} text chunks")
|
| 93 |
|
|
|
|
| 94 |
vector_db = VectorDatabase()
|
| 95 |
vector_db = await vector_db.abuild_from_list(texts)
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
chat_llm = HuggingFaceLLM()
|
| 98 |
+
|
| 99 |
retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
|
| 100 |
vector_db_retriever=vector_db,
|
| 101 |
+
llm=chat_llm
|
| 102 |
)
|
| 103 |
+
|
|
|
|
| 104 |
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
|
| 105 |
await msg.update()
|
| 106 |
|
| 107 |
cl.user_session.set("chain", retrieval_augmented_qa_pipeline)
|
| 108 |
|
|
|
|
| 109 |
@cl.on_message
|
| 110 |
async def main(message):
|
| 111 |
chain = cl.user_session.get("chain")
|
|
|
|
| 116 |
async for stream_resp in result["response"]:
|
| 117 |
await msg.stream_token(stream_resp)
|
| 118 |
|
| 119 |
+
await msg.send()
|