Turbiling commited on
Commit
2b1bf4d
Β·
verified Β·
1 Parent(s): 0785830

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -3,10 +3,10 @@ import chainlit as cl
3
  from groq import Groq
4
  import fitz # PyMuPDF
5
 
6
- # βœ… Initialize Groq Client
7
  groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
8
 
9
- # βœ… Global store for uploaded PDF
10
  pdf_context = ""
11
 
12
  @cl.on_chat_start
@@ -17,7 +17,7 @@ async def on_chat_start():
17
  async def on_message(message: cl.Message):
18
  global pdf_context
19
 
20
- # βœ… Check for uploaded PDF
21
  if message.elements:
22
  for element in message.elements:
23
  if element.name.endswith(".pdf"):
@@ -26,25 +26,27 @@ async def on_message(message: cl.Message):
26
  text = ""
27
  for page in doc:
28
  text += page.get_text()
29
- pdf_context = text[:4000] # limit content
30
- await cl.Message(content="βœ… PDF uploaded! Now ask a question.").send()
31
  return
32
  except Exception as e:
33
  await cl.Message(content=f"❌ Error reading PDF: {e}").send()
34
  return
35
 
36
- # βœ… Check for user message
37
- if not message.content.strip():
 
38
  await cl.Message(content="⚠️ Please enter a question.").send()
39
  return
40
 
41
- # βœ… Prepare prompt
42
- prompt = message.content.strip()
43
  if pdf_context:
44
  prompt = f"Use the following PDF content to answer the question:\n\n{pdf_context}\n\nQuestion: {prompt}"
 
 
45
 
46
  try:
47
- # βœ… Send to Groq
48
  response = groq_client.chat.completions.create(
49
  model="llama3-8b-8192",
50
  messages=[
@@ -53,9 +55,16 @@ async def on_message(message: cl.Message):
53
  ]
54
  )
55
 
56
- # βœ… Extract and send response
57
- answer = response.choices[0].message.content.strip()
58
- await cl.Message(content=answer).send()
 
 
 
 
 
 
 
59
 
60
  except Exception as e:
61
  await cl.Message(content=f"❌ Error: {e}").send()
 
3
  from groq import Groq
4
  import fitz # PyMuPDF
5
 
6
+ # βœ… Initialize Groq client
7
  groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
8
 
9
+ # βœ… Global variable for PDF content
10
  pdf_context = ""
11
 
12
  @cl.on_chat_start
 
17
  async def on_message(message: cl.Message):
18
  global pdf_context
19
 
20
+ # βœ… Handle uploaded PDF
21
  if message.elements:
22
  for element in message.elements:
23
  if element.name.endswith(".pdf"):
 
26
  text = ""
27
  for page in doc:
28
  text += page.get_text()
29
+ pdf_context = text[:4000] # Limit size
30
+ await cl.Message(content="βœ… PDF uploaded! Now ask a question based on it.").send()
31
  return
32
  except Exception as e:
33
  await cl.Message(content=f"❌ Error reading PDF: {e}").send()
34
  return
35
 
36
+ # βœ… Handle text questions
37
+ prompt = message.content.strip()
38
+ if not prompt:
39
  await cl.Message(content="⚠️ Please enter a question.").send()
40
  return
41
 
42
+ # βœ… Append PDF context if available
 
43
  if pdf_context:
44
  prompt = f"Use the following PDF content to answer the question:\n\n{pdf_context}\n\nQuestion: {prompt}"
45
+ else:
46
+ prompt = f"Answer the question:\n\n{prompt}"
47
 
48
  try:
49
+ # βœ… Call Groq API
50
  response = groq_client.chat.completions.create(
51
  model="llama3-8b-8192",
52
  messages=[
 
55
  ]
56
  )
57
 
58
+ # βœ… Print raw response to logs for debugging
59
+ print("=== Groq Raw Response ===")
60
+ print(response)
61
+
62
+ # βœ… Extract and display answer
63
+ if response.choices and response.choices[0].message and response.choices[0].message.content:
64
+ answer = response.choices[0].message.content.strip()
65
+ await cl.Message(content=answer).send()
66
+ else:
67
+ await cl.Message(content="πŸ€– No response from model.").send()
68
 
69
  except Exception as e:
70
  await cl.Message(content=f"❌ Error: {e}").send()