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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -24
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
- # Extract text from uploaded PDF
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!").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()
 
 
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()