Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,48 @@
|
|
| 1 |
import os
|
| 2 |
import chainlit as cl
|
| 3 |
from groq import Groq
|
| 4 |
-
import fitz #
|
| 5 |
|
| 6 |
-
#
|
| 7 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
|
| 12 |
@cl.on_chat_start
|
| 13 |
-
async def
|
| 14 |
-
await cl.Message(content="π Welcome! Upload a PDF or ask
|
| 15 |
|
| 16 |
@cl.on_message
|
| 17 |
-
async def
|
| 18 |
-
global
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
try:
|
| 27 |
response = groq_client.chat.completions.create(
|
| 28 |
model="llama3-8b-8192",
|
| 29 |
messages=[
|
| 30 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 31 |
-
{"role": "user", "content":
|
| 32 |
]
|
| 33 |
)
|
| 34 |
-
|
| 35 |
-
await cl.Message(content=answer).send()
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
await cl.Message(content=f"β Error: {str(e)}").send()
|
| 39 |
-
|
| 40 |
-
@cl.on_file_upload
|
| 41 |
-
async def on_file(file: cl.UploadedFile):
|
| 42 |
-
global uploaded_pdf_text
|
| 43 |
-
try:
|
| 44 |
-
file_path = file.path # Chainlit already saves file and gives safe path
|
| 45 |
-
doc = fitz.open(file_path)
|
| 46 |
-
text = ""
|
| 47 |
-
for page in doc:
|
| 48 |
-
text += page.get_text()
|
| 49 |
-
uploaded_pdf_text = text[:4000] # Limit to 4000 characters
|
| 50 |
-
await cl.Message(content="β
PDF uploaded and processed! Now ask questions about it.").send()
|
| 51 |
-
except Exception as e:
|
| 52 |
-
await cl.Message(content=f"β Failed to read PDF: {str(e)}").send()
|
|
|
|
| 1 |
import os
|
| 2 |
import chainlit as cl
|
| 3 |
from groq import Groq
|
| 4 |
+
import fitz # For reading PDFs
|
| 5 |
|
| 6 |
+
# Initialize Groq client
|
| 7 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
+
# Store uploaded PDF content
|
| 10 |
+
pdf_context = ""
|
| 11 |
|
| 12 |
@cl.on_chat_start
|
| 13 |
+
async def start():
|
| 14 |
+
await cl.Message(content="π Welcome! Upload a PDF or ask a question.").send()
|
| 15 |
|
| 16 |
@cl.on_message
|
| 17 |
+
async def main(message: cl.Message):
|
| 18 |
+
global pdf_context
|
| 19 |
+
|
| 20 |
+
# β
Handle PDF upload (if any)
|
| 21 |
+
if message.elements:
|
| 22 |
+
for element in message.elements:
|
| 23 |
+
if element.name.endswith(".pdf"):
|
| 24 |
+
doc = fitz.open(element.path)
|
| 25 |
+
text = ""
|
| 26 |
+
for page in doc:
|
| 27 |
+
text += page.get_text()
|
| 28 |
+
pdf_context = text[:4000] # Limit content
|
| 29 |
+
await cl.Message(content="β
PDF uploaded! Now ask questions based on it.").send()
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
# β
Handle text questions
|
| 33 |
+
prompt = message.content.strip()
|
| 34 |
+
if pdf_context:
|
| 35 |
+
prompt = f"Use the following PDF content to answer the question:\n\n{pdf_context}\n\nQuestion: {prompt}"
|
| 36 |
|
| 37 |
try:
|
| 38 |
response = groq_client.chat.completions.create(
|
| 39 |
model="llama3-8b-8192",
|
| 40 |
messages=[
|
| 41 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 42 |
+
{"role": "user", "content": prompt}
|
| 43 |
]
|
| 44 |
)
|
| 45 |
+
await cl.Message(content=response.choices[0].message.content).send()
|
|
|
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
await cl.Message(content=f"β Error: {str(e)}").send()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|