Turbiling commited on
Commit
fb01365
Β·
verified Β·
1 Parent(s): 0e98666

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -31
app.py CHANGED
@@ -1,52 +1,48 @@
1
  import os
2
  import chainlit as cl
3
  from groq import Groq
4
- import fitz # PyMuPDF for PDF reading
5
 
6
- # Set up the Groq client with your API key
7
  groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
8
 
9
- # To store uploaded PDF content
10
- uploaded_pdf_text = ""
11
 
12
  @cl.on_chat_start
13
- async def on_chat_start():
14
- await cl.Message(content="πŸ‘‹ Welcome! Upload a PDF or ask me anything.").send()
15
 
16
  @cl.on_message
17
- async def on_message(message: cl.Message):
18
- global uploaded_pdf_text
19
- user_input = message.content.strip()
20
-
21
- # If a PDF was uploaded earlier, combine its content with the user query
22
- full_prompt = user_input
23
- if uploaded_pdf_text:
24
- full_prompt = f"Use this PDF content to answer: {user_input}\n\n{uploaded_pdf_text}"
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  try:
27
  response = groq_client.chat.completions.create(
28
  model="llama3-8b-8192",
29
  messages=[
30
  {"role": "system", "content": "You are a helpful assistant."},
31
- {"role": "user", "content": full_prompt}
32
  ]
33
  )
34
- answer = response.choices[0].message.content
35
- await cl.Message(content=answer).send()
36
 
37
  except Exception as e:
38
  await cl.Message(content=f"❌ Error: {str(e)}").send()
39
-
40
- @cl.on_file_upload
41
- async def on_file(file: cl.UploadedFile):
42
- global uploaded_pdf_text
43
- try:
44
- file_path = file.path # Chainlit already saves file and gives safe path
45
- doc = fitz.open(file_path)
46
- text = ""
47
- for page in doc:
48
- text += page.get_text()
49
- uploaded_pdf_text = text[:4000] # Limit to 4000 characters
50
- await cl.Message(content="βœ… PDF uploaded and processed! Now ask questions about it.").send()
51
- except Exception as e:
52
- await cl.Message(content=f"❌ Failed to read PDF: {str(e)}").send()
 
1
  import os
2
  import chainlit as cl
3
  from groq import Groq
4
+ import fitz # For reading PDFs
5
 
6
+ # Initialize Groq client
7
  groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
8
 
9
+ # Store uploaded PDF content
10
+ pdf_context = ""
11
 
12
  @cl.on_chat_start
13
+ async def start():
14
+ await cl.Message(content="πŸ‘‹ Welcome! Upload a PDF or ask a question.").send()
15
 
16
  @cl.on_message
17
+ async def main(message: cl.Message):
18
+ global pdf_context
19
+
20
+ # βœ… Handle PDF upload (if any)
21
+ if message.elements:
22
+ for element in message.elements:
23
+ if element.name.endswith(".pdf"):
24
+ doc = fitz.open(element.path)
25
+ text = ""
26
+ for page in doc:
27
+ text += page.get_text()
28
+ pdf_context = text[:4000] # Limit content
29
+ await cl.Message(content="βœ… PDF uploaded! Now ask questions based on it.").send()
30
+ return
31
+
32
+ # βœ… Handle text questions
33
+ prompt = message.content.strip()
34
+ if pdf_context:
35
+ prompt = f"Use the following PDF content to answer the question:\n\n{pdf_context}\n\nQuestion: {prompt}"
36
 
37
  try:
38
  response = groq_client.chat.completions.create(
39
  model="llama3-8b-8192",
40
  messages=[
41
  {"role": "system", "content": "You are a helpful assistant."},
42
+ {"role": "user", "content": prompt}
43
  ]
44
  )
45
+ await cl.Message(content=response.choices[0].message.content).send()
 
46
 
47
  except Exception as e:
48
  await cl.Message(content=f"❌ Error: {str(e)}").send()