gizemsarsinlar commited on
Commit
a0376f8
·
verified ·
1 Parent(s): 5fe9221

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -32
app.py CHANGED
@@ -25,47 +25,51 @@ async def start_chat():
25
 
26
  await cl.Message(content="Welcome! Please upload a document to begin.").send()
27
 
28
- # This handles incoming files manually (instead of on_file_upload)
29
- @cl.on_message
30
- async def main(message: cl.Message):
31
- if message.attachments: # Check if the message contains file attachments
32
- uploaded_file = message.attachments[0]
 
33
  file_path = uploaded_file['path']
34
 
35
  # Extract text from the uploaded document
36
  file_content = await extract_text_from_file(file_path)
37
-
38
  # Store document content in user session
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()
 
25
 
26
  await cl.Message(content="Welcome! Please upload a document to begin.").send()
27
 
28
+ # Handling file upload events
29
+ @cl.on_file_upload # Using the proper decorator for file upload handling
30
+ async def on_file_upload(files: list):
31
+ if files:
32
+ # Get the first uploaded file
33
+ uploaded_file = files[0]
34
  file_path = uploaded_file['path']
35
 
36
  # Extract text from the uploaded document
37
  file_content = await extract_text_from_file(file_path)
38
+
39
  # Store document content in user session
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
+ # Handling user queries
45
+ @cl.on_message
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
+ # Create the prompt for OpenAI based on user message and document content
57
+ prompt = f"Document Content: {document_content}\n\nUser Query: {message.content}"
58
+
59
+ msg = cl.Message(content="")
60
 
61
+ # Send prompt to OpenAI and stream response
62
+ async for stream_resp in await client.chat.completions.create(
63
+ model=settings["model"],
64
+ messages=[{"role": "system", "content": "Answer based on the provided document."},
65
+ {"role": "user", "content": prompt}],
66
+ stream=True,
67
+ **settings,
68
+ ):
69
+ token = stream_resp.choices[0].delta.content
70
+ if not token:
71
+ token = ""
72
+ await msg.stream_token(token)
73
 
74
+ # Send final response
75
+ await msg.send()