Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,10 @@ import os
|
|
| 2 |
import chainlit as cl
|
| 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"))
|
|
@@ -11,12 +15,15 @@ pdf_context = ""
|
|
| 11 |
|
| 12 |
@cl.on_chat_start
|
| 13 |
async def on_chat_start():
|
|
|
|
| 14 |
await cl.Message(content="π Welcome! Upload a PDF or ask a question.").send()
|
| 15 |
|
| 16 |
@cl.on_message
|
| 17 |
async def on_message(message: cl.Message):
|
| 18 |
global pdf_context
|
| 19 |
|
|
|
|
|
|
|
| 20 |
# β
Handle PDF upload
|
| 21 |
if message.elements:
|
| 22 |
for element in message.elements:
|
|
@@ -26,39 +33,45 @@ 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 |
prompt = message.content.strip()
|
| 38 |
if not prompt:
|
| 39 |
await cl.Message(content="β οΈ Please enter a question.").send()
|
| 40 |
return
|
| 41 |
|
| 42 |
-
# β
|
|
|
|
| 43 |
if pdf_context:
|
| 44 |
-
|
| 45 |
|
| 46 |
try:
|
| 47 |
-
# β
|
| 48 |
-
def
|
| 49 |
return groq_client.chat.completions.create(
|
| 50 |
model="llama3-8b-8192",
|
| 51 |
messages=[
|
| 52 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 53 |
-
{"role": "user", "content":
|
| 54 |
]
|
| 55 |
)
|
| 56 |
|
| 57 |
-
response = await cl.run_sync(
|
|
|
|
| 58 |
answer = response.choices[0].message.content.strip()
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
await cl.Message(content=answer).send()
|
| 62 |
|
| 63 |
except Exception as e:
|
|
|
|
| 64 |
await cl.Message(content=f"β Error: {e}").send()
|
|
|
|
| 2 |
import chainlit as cl
|
| 3 |
from groq import Groq
|
| 4 |
import fitz # PyMuPDF
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# β
Logging for debug (appears in HF logs)
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
|
| 10 |
# β
Initialize Groq client
|
| 11 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
|
|
| 15 |
|
| 16 |
@cl.on_chat_start
|
| 17 |
async def on_chat_start():
|
| 18 |
+
logging.info("Chat started.")
|
| 19 |
await cl.Message(content="π Welcome! Upload a PDF or ask a question.").send()
|
| 20 |
|
| 21 |
@cl.on_message
|
| 22 |
async def on_message(message: cl.Message):
|
| 23 |
global pdf_context
|
| 24 |
|
| 25 |
+
logging.info(f"User message received: {message.content}")
|
| 26 |
+
|
| 27 |
# β
Handle PDF upload
|
| 28 |
if message.elements:
|
| 29 |
for element in message.elements:
|
|
|
|
| 33 |
text = ""
|
| 34 |
for page in doc:
|
| 35 |
text += page.get_text()
|
| 36 |
+
pdf_context = text[:4000]
|
| 37 |
await cl.Message(content="β
PDF uploaded! Now ask a question.").send()
|
| 38 |
return
|
| 39 |
except Exception as e:
|
| 40 |
+
logging.error(f"Error reading PDF: {e}")
|
| 41 |
await cl.Message(content=f"β Error reading PDF: {e}").send()
|
| 42 |
return
|
| 43 |
|
| 44 |
+
# β
Validate user message
|
| 45 |
prompt = message.content.strip()
|
| 46 |
if not prompt:
|
| 47 |
await cl.Message(content="β οΈ Please enter a question.").send()
|
| 48 |
return
|
| 49 |
|
| 50 |
+
# β
Append PDF context if available
|
| 51 |
+
full_prompt = prompt
|
| 52 |
if pdf_context:
|
| 53 |
+
full_prompt = f"Use the following PDF content to answer the question:\n\n{pdf_context}\n\nQuestion: {prompt}"
|
| 54 |
|
| 55 |
try:
|
| 56 |
+
# β
Wrap sync Groq call in async function
|
| 57 |
+
def generate_response():
|
| 58 |
return groq_client.chat.completions.create(
|
| 59 |
model="llama3-8b-8192",
|
| 60 |
messages=[
|
| 61 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 62 |
+
{"role": "user", "content": full_prompt}
|
| 63 |
]
|
| 64 |
)
|
| 65 |
|
| 66 |
+
response = await cl.run_sync(generate_response)
|
| 67 |
+
|
| 68 |
answer = response.choices[0].message.content.strip()
|
| 69 |
|
| 70 |
+
logging.info(f"LLM response: {answer[:100]}...")
|
| 71 |
+
|
| 72 |
+
# β
Display response
|
| 73 |
await cl.Message(content=answer).send()
|
| 74 |
|
| 75 |
except Exception as e:
|
| 76 |
+
logging.error(f"β Error during LLM response: {e}")
|
| 77 |
await cl.Message(content=f"β Error: {e}").send()
|