Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,10 @@ import chainlit as cl
|
|
| 3 |
from groq import Groq
|
| 4 |
import fitz # PyMuPDF
|
| 5 |
|
| 6 |
-
# β
Initialize Groq
|
| 7 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
-
# β
Global
|
| 10 |
pdf_context = ""
|
| 11 |
|
| 12 |
@cl.on_chat_start
|
|
@@ -17,7 +17,7 @@ async def on_chat_start():
|
|
| 17 |
async def on_message(message: cl.Message):
|
| 18 |
global pdf_context
|
| 19 |
|
| 20 |
-
# β
|
| 21 |
if message.elements:
|
| 22 |
for element in message.elements:
|
| 23 |
if element.name.endswith(".pdf"):
|
|
@@ -26,25 +26,27 @@ async def on_message(message: cl.Message):
|
|
| 26 |
text = ""
|
| 27 |
for page in doc:
|
| 28 |
text += page.get_text()
|
| 29 |
-
pdf_context = text[:4000] #
|
| 30 |
-
await cl.Message(content="β
PDF uploaded! Now ask a question.").send()
|
| 31 |
return
|
| 32 |
except Exception as e:
|
| 33 |
await cl.Message(content=f"β Error reading PDF: {e}").send()
|
| 34 |
return
|
| 35 |
|
| 36 |
-
# β
|
| 37 |
-
|
|
|
|
| 38 |
await cl.Message(content="β οΈ Please enter a question.").send()
|
| 39 |
return
|
| 40 |
|
| 41 |
-
# β
|
| 42 |
-
prompt = message.content.strip()
|
| 43 |
if pdf_context:
|
| 44 |
prompt = f"Use the following PDF content to answer the question:\n\n{pdf_context}\n\nQuestion: {prompt}"
|
|
|
|
|
|
|
| 45 |
|
| 46 |
try:
|
| 47 |
-
# β
|
| 48 |
response = groq_client.chat.completions.create(
|
| 49 |
model="llama3-8b-8192",
|
| 50 |
messages=[
|
|
@@ -53,9 +55,16 @@ async def on_message(message: cl.Message):
|
|
| 53 |
]
|
| 54 |
)
|
| 55 |
|
| 56 |
-
# β
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
await cl.Message(content=f"β Error: {e}").send()
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
import fitz # PyMuPDF
|
| 5 |
|
| 6 |
+
# β
Initialize Groq client
|
| 7 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
+
# β
Global variable for PDF content
|
| 10 |
pdf_context = ""
|
| 11 |
|
| 12 |
@cl.on_chat_start
|
|
|
|
| 17 |
async def on_message(message: cl.Message):
|
| 18 |
global pdf_context
|
| 19 |
|
| 20 |
+
# β
Handle uploaded PDF
|
| 21 |
if message.elements:
|
| 22 |
for element in message.elements:
|
| 23 |
if element.name.endswith(".pdf"):
|
|
|
|
| 26 |
text = ""
|
| 27 |
for page in doc:
|
| 28 |
text += page.get_text()
|
| 29 |
+
pdf_context = text[:4000] # Limit size
|
| 30 |
+
await cl.Message(content="β
PDF uploaded! Now ask a question based on it.").send()
|
| 31 |
return
|
| 32 |
except Exception as e:
|
| 33 |
await cl.Message(content=f"β Error reading PDF: {e}").send()
|
| 34 |
return
|
| 35 |
|
| 36 |
+
# β
Handle text questions
|
| 37 |
+
prompt = message.content.strip()
|
| 38 |
+
if not prompt:
|
| 39 |
await cl.Message(content="β οΈ Please enter a question.").send()
|
| 40 |
return
|
| 41 |
|
| 42 |
+
# β
Append PDF context if available
|
|
|
|
| 43 |
if pdf_context:
|
| 44 |
prompt = f"Use the following PDF content to answer the question:\n\n{pdf_context}\n\nQuestion: {prompt}"
|
| 45 |
+
else:
|
| 46 |
+
prompt = f"Answer the question:\n\n{prompt}"
|
| 47 |
|
| 48 |
try:
|
| 49 |
+
# β
Call Groq API
|
| 50 |
response = groq_client.chat.completions.create(
|
| 51 |
model="llama3-8b-8192",
|
| 52 |
messages=[
|
|
|
|
| 55 |
]
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# β
Print raw response to logs for debugging
|
| 59 |
+
print("=== Groq Raw Response ===")
|
| 60 |
+
print(response)
|
| 61 |
+
|
| 62 |
+
# β
Extract and display answer
|
| 63 |
+
if response.choices and response.choices[0].message and response.choices[0].message.content:
|
| 64 |
+
answer = response.choices[0].message.content.strip()
|
| 65 |
+
await cl.Message(content=answer).send()
|
| 66 |
+
else:
|
| 67 |
+
await cl.Message(content="π€ No response from model.").send()
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
await cl.Message(content=f"β Error: {e}").send()
|