Turbiling commited on
Commit
49c7142
Β·
verified Β·
1 Parent(s): c1bba2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -18
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="πŸ‘‹ Hello! I am your Groq-powered chatbot.").send()
10
 
11
  @cl.on_message
12
  async def handle_message(message: cl.Message):
13
- print("πŸ“₯ Received message:", message.content) # Debug log
 
 
 
 
14
 
15
- try:
16
- response = groq_client.chat.completions.create(
17
- model="llama3-8b-8192",
18
- messages=[
19
- {"role": "system", "content": "You are a helpful assistant."},
20
- {"role": "user", "content": message.content}
21
- ]
22
- )
23
- reply = response.choices[0].message.content
24
- print("πŸ“€ Groq response:", reply) # Debug log
25
- await cl.Message(content=reply).send()
 
 
26
 
27
- except Exception as e:
28
- error_msg = f"❌ Error: {str(e)}"
29
- print(error_msg)
30
- await cl.Message(content=error_msg).send()
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()