Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,59 @@
|
|
| 1 |
import chainlit as cl
|
| 2 |
import os
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
|
|
|
| 5 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
@cl.on_chat_start
|
| 8 |
async def start_chat():
|
| 9 |
-
await cl.Message(content="π
|
| 10 |
|
| 11 |
@cl.on_message
|
| 12 |
async def handle_message(message: cl.Message):
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import chainlit as cl
|
| 2 |
import os
|
| 3 |
+
import fitz # PyMuPDF
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
+
# Initialize Groq client
|
| 7 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
+
# Extract text from uploaded PDF
|
| 10 |
+
def extract_pdf_text(file_path):
|
| 11 |
+
text = ""
|
| 12 |
+
with fitz.open(file_path) as doc:
|
| 13 |
+
for page in doc:
|
| 14 |
+
text += page.get_text()
|
| 15 |
+
return text
|
| 16 |
+
|
| 17 |
@cl.on_chat_start
|
| 18 |
async def start_chat():
|
| 19 |
+
await cl.Message(content="π Upload a PDF or ask me anything!").send()
|
| 20 |
|
| 21 |
@cl.on_message
|
| 22 |
async def handle_message(message: cl.Message):
|
| 23 |
+
# If user uploaded a file
|
| 24 |
+
if message.elements:
|
| 25 |
+
file = message.elements[0]
|
| 26 |
+
file_path = await file.download()
|
| 27 |
+
await cl.Message(content=f"π Received file: {file.name}").send()
|
| 28 |
|
| 29 |
+
try:
|
| 30 |
+
# Extract text
|
| 31 |
+
pdf_text = extract_pdf_text(file_path)
|
| 32 |
+
# Ask Groq a question about the content
|
| 33 |
+
response = groq_client.chat.completions.create(
|
| 34 |
+
model="llama3-8b-8192",
|
| 35 |
+
messages=[
|
| 36 |
+
{"role": "system", "content": "You are a PDF-reading assistant. Answer questions based on the document."},
|
| 37 |
+
{"role": "user", "content": f"Summarize this PDF:\n{pdf_text[:4000]}"}
|
| 38 |
+
]
|
| 39 |
+
)
|
| 40 |
+
reply = response.choices[0].message.content
|
| 41 |
+
await cl.Message(content=f"π§ Summary:\n{reply}").send()
|
| 42 |
|
| 43 |
+
except Exception as e:
|
| 44 |
+
await cl.Message(content=f"β PDF error: {str(e)}").send()
|
| 45 |
+
|
| 46 |
+
# If user typed a question
|
| 47 |
+
else:
|
| 48 |
+
try:
|
| 49 |
+
response = 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": message.content}
|
| 54 |
+
]
|
| 55 |
+
)
|
| 56 |
+
reply = response.choices[0].message.content
|
| 57 |
+
await cl.Message(content=reply).send()
|
| 58 |
+
except Exception as e:
|
| 59 |
+
await cl.Message(content=f"β Error: {str(e)}").send()
|