Turbiling commited on
Commit
ef1ea95
Β·
verified Β·
1 Parent(s): 2069031

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -2,6 +2,10 @@ import os
2
  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"))
@@ -11,12 +15,15 @@ pdf_context = ""
11
 
12
  @cl.on_chat_start
13
  async def on_chat_start():
 
14
  await cl.Message(content="πŸ‘‹ Welcome! Upload a PDF or ask a question.").send()
15
 
16
  @cl.on_message
17
  async def on_message(message: cl.Message):
18
  global pdf_context
19
 
 
 
20
  # βœ… Handle PDF upload
21
  if message.elements:
22
  for element in message.elements:
@@ -26,39 +33,45 @@ 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 text if needed
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
- # βœ… Prepare user question
37
  prompt = message.content.strip()
38
  if not prompt:
39
  await cl.Message(content="⚠️ Please enter a question.").send()
40
  return
41
 
42
- # βœ… Add 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
 
46
  try:
47
- # βœ… Safely run sync LLM in async app
48
- def run_groq():
49
  return 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": prompt}
54
  ]
55
  )
56
 
57
- response = await cl.run_sync(run_groq)
 
58
  answer = response.choices[0].message.content.strip()
59
 
60
- # βœ… Show answer in the UI
 
 
61
  await cl.Message(content=answer).send()
62
 
63
  except Exception as e:
 
64
  await cl.Message(content=f"❌ Error: {e}").send()
 
2
  import chainlit as cl
3
  from groq import Groq
4
  import fitz # PyMuPDF
5
+ import logging
6
+
7
+ # βœ… Logging for debug (appears in HF logs)
8
+ logging.basicConfig(level=logging.INFO)
9
 
10
  # βœ… Initialize Groq client
11
  groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
15
 
16
  @cl.on_chat_start
17
  async def on_chat_start():
18
+ logging.info("Chat started.")
19
  await cl.Message(content="πŸ‘‹ Welcome! Upload a PDF or ask a question.").send()
20
 
21
  @cl.on_message
22
  async def on_message(message: cl.Message):
23
  global pdf_context
24
 
25
+ logging.info(f"User message received: {message.content}")
26
+
27
  # βœ… Handle PDF upload
28
  if message.elements:
29
  for element in message.elements:
 
33
  text = ""
34
  for page in doc:
35
  text += page.get_text()
36
+ pdf_context = text[:4000]
37
  await cl.Message(content="βœ… PDF uploaded! Now ask a question.").send()
38
  return
39
  except Exception as e:
40
+ logging.error(f"Error reading PDF: {e}")
41
  await cl.Message(content=f"❌ Error reading PDF: {e}").send()
42
  return
43
 
44
+ # βœ… Validate user message
45
  prompt = message.content.strip()
46
  if not prompt:
47
  await cl.Message(content="⚠️ Please enter a question.").send()
48
  return
49
 
50
+ # βœ… Append PDF context if available
51
+ full_prompt = prompt
52
  if pdf_context:
53
+ full_prompt = f"Use the following PDF content to answer the question:\n\n{pdf_context}\n\nQuestion: {prompt}"
54
 
55
  try:
56
+ # βœ… Wrap sync Groq call in async function
57
+ def generate_response():
58
  return groq_client.chat.completions.create(
59
  model="llama3-8b-8192",
60
  messages=[
61
  {"role": "system", "content": "You are a helpful assistant."},
62
+ {"role": "user", "content": full_prompt}
63
  ]
64
  )
65
 
66
+ response = await cl.run_sync(generate_response)
67
+
68
  answer = response.choices[0].message.content.strip()
69
 
70
+ logging.info(f"LLM response: {answer[:100]}...")
71
+
72
+ # βœ… Display response
73
  await cl.Message(content=answer).send()
74
 
75
  except Exception as e:
76
+ logging.error(f"❌ Error during LLM response: {e}")
77
  await cl.Message(content=f"❌ Error: {e}").send()