Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import fitz # PyMuPDF
|
|
| 6 |
# β
Initialize Groq client
|
| 7 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
-
# β
Global variable
|
| 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,27 +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] # Limit
|
| 30 |
-
await cl.Message(content="β
PDF uploaded! Now ask a question
|
| 31 |
return
|
| 32 |
except Exception as e:
|
| 33 |
await cl.Message(content=f"β Error reading PDF: {e}").send()
|
| 34 |
return
|
| 35 |
|
| 36 |
-
# β
|
| 37 |
-
|
| 38 |
-
if not prompt:
|
| 39 |
await cl.Message(content="β οΈ Please enter a question.").send()
|
| 40 |
return
|
| 41 |
|
| 42 |
-
# β
|
|
|
|
| 43 |
if pdf_context:
|
| 44 |
-
prompt = f"Use the following PDF content to answer the question:\n\n{pdf_context}\n\nQuestion: {
|
| 45 |
else:
|
| 46 |
-
prompt =
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
# β
|
| 50 |
response = groq_client.chat.completions.create(
|
| 51 |
model="llama3-8b-8192",
|
| 52 |
messages=[
|
|
@@ -55,16 +55,18 @@ async def on_message(message: cl.Message):
|
|
| 55 |
]
|
| 56 |
)
|
| 57 |
|
| 58 |
-
# β
|
| 59 |
print("=== Groq Raw Response ===")
|
| 60 |
print(response)
|
| 61 |
|
| 62 |
-
# β
Extract
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
-
await cl.Message(content=f"β Error: {e}").send()
|
|
|
|
| 6 |
# β
Initialize Groq client
|
| 7 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
+
# β
Global variable to store 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 |
+
# β
Check for 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 context to 4000 characters
|
| 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 |
+
# β
If no question was asked
|
| 37 |
+
if not message.content.strip():
|
|
|
|
| 38 |
await cl.Message(content="β οΈ Please enter a question.").send()
|
| 39 |
return
|
| 40 |
|
| 41 |
+
# β
Construct prompt
|
| 42 |
+
user_question = 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: {user_question}"
|
| 45 |
else:
|
| 46 |
+
prompt = user_question
|
| 47 |
|
| 48 |
try:
|
| 49 |
+
# β
Send request to Groq
|
| 50 |
response = groq_client.chat.completions.create(
|
| 51 |
model="llama3-8b-8192",
|
| 52 |
messages=[
|
|
|
|
| 55 |
]
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# β
Log raw response for debugging
|
| 59 |
print("=== Groq Raw Response ===")
|
| 60 |
print(response)
|
| 61 |
|
| 62 |
+
# β
Extract assistant reply safely
|
| 63 |
if response.choices and response.choices[0].message and response.choices[0].message.content:
|
| 64 |
answer = response.choices[0].message.content.strip()
|
|
|
|
| 65 |
else:
|
| 66 |
+
answer = "π€ No response received from model."
|
| 67 |
+
|
| 68 |
+
# β
Show in UI
|
| 69 |
+
await cl.Message(content=answer).send()
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
+
await cl.Message(content=f"β Error: {str(e)}").send()
|