Spaces:
Sleeping
Sleeping
gizemsarsinlar
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -25,12 +25,11 @@ async def start_chat():
|
|
25 |
|
26 |
await cl.Message(content="Welcome! Please upload a document to begin.").send()
|
27 |
|
28 |
-
# Handling file upload
|
29 |
-
@cl.
|
30 |
-
async def
|
31 |
-
if files:
|
32 |
-
|
33 |
-
uploaded_file = files[0]
|
34 |
file_path = uploaded_file['path']
|
35 |
|
36 |
# Extract text from the uploaded document
|
@@ -40,36 +39,33 @@ async def on_file_upload(files: list):
|
|
40 |
cl.user_session.set("document_content", file_content)
|
41 |
|
42 |
await cl.Message(content=f"Document '{uploaded_file['name']}' uploaded successfully! You can now ask questions based on the document content.").send()
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
async def main(message: cl.Message):
|
47 |
-
document_content = cl.user_session.get("document_content", "")
|
48 |
-
|
49 |
-
if not document_content:
|
50 |
-
await cl.Message(content="Please upload a document first.").send()
|
51 |
-
return
|
52 |
-
|
53 |
-
settings = cl.user_session.get("settings")
|
54 |
-
client = AsyncOpenAI()
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
|
|
25 |
|
26 |
await cl.Message(content="Welcome! Please upload a document to begin.").send()
|
27 |
|
28 |
+
# Handling user queries and expecting file upload as part of the message
|
29 |
+
@cl.on_message
|
30 |
+
async def main(message: cl.Message):
|
31 |
+
if message.files: # Check if the message contains any files
|
32 |
+
uploaded_file = message.files[0]
|
|
|
33 |
file_path = uploaded_file['path']
|
34 |
|
35 |
# Extract text from the uploaded document
|
|
|
39 |
cl.user_session.set("document_content", file_content)
|
40 |
|
41 |
await cl.Message(content=f"Document '{uploaded_file['name']}' uploaded successfully! You can now ask questions based on the document content.").send()
|
42 |
+
else:
|
43 |
+
document_content = cl.user_session.get("document_content", "")
|
44 |
+
|
45 |
+
if not document_content:
|
46 |
+
await cl.Message(content="Please upload a document first.").send()
|
47 |
+
return
|
48 |
|
49 |
+
settings = cl.user_session.get("settings")
|
50 |
+
client = AsyncOpenAI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
# Create the prompt for OpenAI based on user message and document content
|
53 |
+
prompt = f"Document Content: {document_content}\n\nUser Query: {message.content}"
|
54 |
+
|
55 |
+
msg = cl.Message(content="")
|
56 |
|
57 |
+
# Send prompt to OpenAI and stream response
|
58 |
+
async for stream_resp in await client.chat.completions.create(
|
59 |
+
model=settings["model"],
|
60 |
+
messages=[{"role": "system", "content": "Answer based on the provided document."},
|
61 |
+
{"role": "user", "content": prompt}],
|
62 |
+
stream=True,
|
63 |
+
**settings,
|
64 |
+
):
|
65 |
+
token = stream_resp.choices[0].delta.content
|
66 |
+
if not token:
|
67 |
+
token = ""
|
68 |
+
await msg.stream_token(token)
|
69 |
|
70 |
+
# Send final response
|
71 |
+
await msg.send()
|