Turbiling commited on
Commit
42be3b7
Β·
verified Β·
1 Parent(s): 25ce54b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -1,12 +1,12 @@
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
- # Function to extract text from PDF
10
  def extract_pdf_text(file_path):
11
  text = ""
12
  with fitz.open(file_path) as doc:
@@ -16,34 +16,35 @@ def extract_pdf_text(file_path):
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
 
@@ -56,8 +57,8 @@ async def handle_message(message: cl.Message):
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()
63
-
 
1
  import chainlit as cl
2
  import os
3
+ import fitz # PyMuPDF for PDF reading
4
  from groq import Groq
5
 
6
+ # Initialize Groq client using your Hugging Face Secret
7
  groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
8
 
9
+ # Function to extract text from PDF file
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 any question.").send()
20
 
21
  @cl.on_message
22
  async def handle_message(message: cl.Message):
23
  try:
24
+ # If user uploaded a file (PDF)
25
  if message.elements:
26
  file = message.elements[0]
27
+ file_path = file.path # Access the uploaded file path directly
28
+ await cl.Message(content=f"πŸ“„ File uploaded: `{file.name}`").send()
29
 
30
+ # Extract PDF text
31
  pdf_text = extract_pdf_text(file_path)
32
 
33
+ # Ask Groq to summarize it
34
  response = groq_client.chat.completions.create(
35
  model="llama3-8b-8192",
36
  messages=[
37
+ {"role": "system", "content": "Summarize the uploaded PDF content."},
38
+ {"role": "user", "content": pdf_text[:4000]} # limit to 4k tokens
39
  ]
40
  )
41
 
42
  reply = response.choices[0].message.content
43
+ await cl.sleep(1) # Fix for Chainlit session issue
44
  await cl.Message(content=f"πŸ“š Summary:\n{reply}").send()
45
 
46
+ # If it's a normal question
47
  else:
 
48
  user_input = message.content
49
  await cl.Message(content=f"πŸ“ Question: {user_input}").send()
50
 
 
57
  )
58
 
59
  reply = response.choices[0].message.content
60
+ await cl.sleep(1) # Fix for Chainlit session issue
61
  await cl.Message(content=reply).send()
62
 
63
  except Exception as e:
64
  await cl.Message(content=f"❌ Error: {str(e)}").send()