Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from groq import Groq
|
|
| 6 |
# Initialize Groq client
|
| 7 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
-
#
|
| 10 |
def extract_pdf_text(file_path):
|
| 11 |
text = ""
|
| 12 |
with fitz.open(file_path) as doc:
|
|
@@ -16,44 +16,47 @@ def extract_pdf_text(file_path):
|
|
| 16 |
|
| 17 |
@cl.on_chat_start
|
| 18 |
async def start_chat():
|
| 19 |
-
await cl.Message(content="π Upload a PDF or ask me anything
|
| 20 |
|
| 21 |
@cl.on_message
|
| 22 |
async def handle_message(message: cl.Message):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# Extract text
|
| 31 |
pdf_text = extract_pdf_text(file_path)
|
| 32 |
-
|
|
|
|
| 33 |
response = groq_client.chat.completions.create(
|
| 34 |
model="llama3-8b-8192",
|
| 35 |
messages=[
|
| 36 |
-
{"role": "system", "content": "
|
| 37 |
-
{"role": "user", "content":
|
| 38 |
]
|
| 39 |
)
|
|
|
|
| 40 |
reply = response.choices[0].message.content
|
| 41 |
-
await cl.Message(content=f"
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 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":
|
| 54 |
]
|
| 55 |
)
|
|
|
|
| 56 |
reply = response.choices[0].message.content
|
| 57 |
await cl.Message(content=reply).send()
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
| 6 |
# Initialize Groq client
|
| 7 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
+
# Function to extract text from PDF
|
| 10 |
def extract_pdf_text(file_path):
|
| 11 |
text = ""
|
| 12 |
with fitz.open(file_path) as doc:
|
|
|
|
| 16 |
|
| 17 |
@cl.on_chat_start
|
| 18 |
async def start_chat():
|
| 19 |
+
await cl.Message(content="π Welcome! Upload a PDF or ask me anything.").send()
|
| 20 |
|
| 21 |
@cl.on_message
|
| 22 |
async def handle_message(message: cl.Message):
|
| 23 |
+
try:
|
| 24 |
+
# β
Check if user uploaded a file
|
| 25 |
+
if message.elements:
|
| 26 |
+
file = message.elements[0]
|
| 27 |
+
file_path = file.path # β
Use `file.path` directly β no download
|
| 28 |
+
await cl.Message(content=f"π File uploaded: {file.name}").send()
|
| 29 |
+
|
| 30 |
+
# β
Extract text from PDF
|
| 31 |
pdf_text = extract_pdf_text(file_path)
|
| 32 |
+
|
| 33 |
+
# β
Ask Groq to summarize the content
|
| 34 |
response = groq_client.chat.completions.create(
|
| 35 |
model="llama3-8b-8192",
|
| 36 |
messages=[
|
| 37 |
+
{"role": "system", "content": "Summarize the uploaded PDF."},
|
| 38 |
+
{"role": "user", "content": pdf_text[:4000]} # Limit to 4k tokens
|
| 39 |
]
|
| 40 |
)
|
| 41 |
+
|
| 42 |
reply = response.choices[0].message.content
|
| 43 |
+
await cl.Message(content=f"π Summary:\n{reply}").send()
|
| 44 |
+
|
| 45 |
+
else:
|
| 46 |
+
# β
Handle normal text questions
|
| 47 |
+
user_input = message.content
|
| 48 |
+
await cl.Message(content=f"π Question: {user_input}").send()
|
| 49 |
+
|
|
|
|
| 50 |
response = groq_client.chat.completions.create(
|
| 51 |
model="llama3-8b-8192",
|
| 52 |
messages=[
|
| 53 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 54 |
+
{"role": "user", "content": user_input}
|
| 55 |
]
|
| 56 |
)
|
| 57 |
+
|
| 58 |
reply = response.choices[0].message.content
|
| 59 |
await cl.Message(content=reply).send()
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
await cl.Message(content=f"β Error: {str(e)}").send()
|