Spaces:
Sleeping
Sleeping
Download app.py from Turbiling/Chatbot_Chainlit: direct link, hf CLI and curl.
- Browser
- Download file 2.53 kB
-
https://huggingface.co/spaces/Turbiling/Chatbot_Chainlit/resolve/ef1ea95c2452f171d94de4bda333834ec7e10cb9/app.py
- Command line
-
hf download hf://spaces/Turbiling/Chatbot_Chainlit@ef1ea95c2452f171d94de4bda333834ec7e10cb9/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Turbiling/Chatbot_Chainlit/resolve/ef1ea95c2452f171d94de4bda333834ec7e10cb9/app.py
2.53 kB
| 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 = "" | |
| async def on_chat_start(): | |
| logging.info("Chat started.") | |
| await cl.Message(content="π Welcome! Upload a PDF or ask a question.").send() | |
| 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() | |