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