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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -6,7 +6,7 @@ import fitz # PyMuPDF
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,7 +17,7 @@ async def 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,27 +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 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,16 +55,18 @@ async def on_message(message: cl.Message):
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()
 
6
  # βœ… Initialize Groq client
7
  groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
8
 
9
+ # βœ… Global variable to store 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
+ # βœ… Check for 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 context to 4000 characters
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
+ # βœ… If no question was asked
37
+ if not message.content.strip():
 
38
  await cl.Message(content="⚠️ Please enter a question.").send()
39
  return
40
 
41
+ # βœ… Construct prompt
42
+ user_question = 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: {user_question}"
45
  else:
46
+ prompt = user_question
47
 
48
  try:
49
+ # βœ… Send request to Groq
50
  response = groq_client.chat.completions.create(
51
  model="llama3-8b-8192",
52
  messages=[
 
55
  ]
56
  )
57
 
58
+ # βœ… Log raw response for debugging
59
  print("=== Groq Raw Response ===")
60
  print(response)
61
 
62
+ # βœ… Extract assistant reply safely
63
  if response.choices and response.choices[0].message and response.choices[0].message.content:
64
  answer = response.choices[0].message.content.strip()
 
65
  else:
66
+ answer = "πŸ€– No response received from model."
67
+
68
+ # βœ… Show in UI
69
+ await cl.Message(content=answer).send()
70
 
71
  except Exception as e:
72
+ await cl.Message(content=f"❌ Error: {str(e)}").send()