masadonline commited on
Commit
7ef0563
·
verified ·
1 Parent(s): 4a205e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -34
app.py CHANGED
@@ -13,6 +13,7 @@ from groq import Groq
13
  import PyPDF2
14
  import requests
15
 
 
16
  # --- Document Loaders ---
17
  def extract_text_from_pdf(pdf_path):
18
  try:
@@ -50,42 +51,34 @@ def retrieve_chunks(question, index, embed_model, text_chunks, k=3):
50
  D, I = index.search(np.array([question_embedding]), k)
51
  return [text_chunks[i] for i in I[0]]
52
 
53
- #def generate_answer_with_groq(question, context, retries=3, delay=2):
54
- # prompt = f"Based on the following context, answer the question: '{question}'\n\nContext:\n{context}"
55
- # groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
56
-
57
- # for attempt in range(retries):
58
- # try:
59
- # response = groq_client.chat.completions.create(
60
- # model="llama3-8b-8192",
61
- # messages=[
62
- # {"role": "system", "content": "You are an AI Assistant for Small Businesses."},
63
- # {"role": "user", "content": prompt},
64
- # ]
65
- # )
66
- # return response.choices[0].message.content
67
- # except GroqError as e:
68
- # if "503" in str(e) and attempt < retries - 1:
69
- # time.sleep(delay)
70
- # continue
71
- # else:
72
- # return f"⚠️ Groq API Error: {str(e)}"
73
-
74
- #-----------------------------------------
75
- import requests # ✅ Add this at the top of your file
76
-
77
  def generate_answer_with_groq(question, context, retries=3, delay=2):
78
  url = "https://api.groq.com/openai/v1/chat/completions"
79
- api_key = os.environ["GROQ_API_KEY"]
 
 
 
80
  headers = {
81
  "Authorization": f"Bearer {api_key}",
82
  "Content-Type": "application/json",
83
  }
84
- prompt = f"Based on the following context, answer the question: '{question}'\n\nContext:\n{context}"
 
 
 
 
85
  payload = {
86
  "model": "llama3-8b-8192",
87
  "messages": [
88
- {"role": "system", "content": "You are a real human and you will respond as real human. You will be asked questions and you will respond like a human."},
 
 
 
 
 
 
 
 
89
  {"role": "user", "content": prompt},
90
  ],
91
  "temperature": 0.5,
@@ -94,17 +87,18 @@ def generate_answer_with_groq(question, context, retries=3, delay=2):
94
 
95
  for attempt in range(retries):
96
  try:
97
- response = requests.post(url, headers=headers, json=payload)
 
98
  result = response.json()
99
  return result['choices'][0]['message']['content'].strip()
100
- except Exception as e:
101
- if "503" in str(e) and attempt < retries - 1:
102
  time.sleep(delay)
103
  continue
104
  else:
105
- return f"⚠️ Groq API Error: {str(e)}"
106
-
107
- #-----------------------------------------
108
 
109
  # --- Twilio Chat Handlers ---
110
  def fetch_latest_incoming_message(account_sid, auth_token, conversation_sid):
@@ -130,7 +124,7 @@ st.title("📱 SMEHelpBot + WhatsApp (via Twilio)")
130
  # Load from Hugging Face secrets
131
  account_sid = st.secrets.get("TWILIO_SID")
132
  auth_token = st.secrets.get("TWILIO_TOKEN")
133
- conversation_sid = "CHe065d01a6b654ca0a698bfb849c980de"
134
  GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
135
 
136
  # Fallback for testing
 
13
  import PyPDF2
14
  import requests
15
 
16
+ # Extract text from PDF with fallback
17
  # --- Document Loaders ---
18
  def extract_text_from_pdf(pdf_path):
19
  try:
 
51
  D, I = index.search(np.array([question_embedding]), k)
52
  return [text_chunks[i] for i in I[0]]
53
 
54
+ # Generate answer using Groq API with retries and timeout
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  def generate_answer_with_groq(question, context, retries=3, delay=2):
56
  url = "https://api.groq.com/openai/v1/chat/completions"
57
+ api_key = os.environ.get("GROQ_API_KEY")
58
+ if not api_key:
59
+ return "⚠️ GROQ_API_KEY not set."
60
+
61
  headers = {
62
  "Authorization": f"Bearer {api_key}",
63
  "Content-Type": "application/json",
64
  }
65
+ prompt = (
66
+ f"Customer asked: '{question}'\n\n"
67
+ f"Here is the relevant product or policy info to help:\n{context}\n\n"
68
+ f"Respond in a friendly and helpful tone as a toy shop support agent."
69
+ )
70
  payload = {
71
  "model": "llama3-8b-8192",
72
  "messages": [
73
+ {
74
+ "role": "system",
75
+ "content": (
76
+ "You are ToyBot, a friendly and helpful WhatsApp assistant for an online toy shop. "
77
+ "Your goal is to politely answer customer questions, help them choose the right toys, "
78
+ "provide order or delivery information, explain return policies, and guide them through purchases. "
79
+ "Always sound warm, helpful, and trustworthy like a professional customer support agent."
80
+ )
81
+ },
82
  {"role": "user", "content": prompt},
83
  ],
84
  "temperature": 0.5,
 
87
 
88
  for attempt in range(retries):
89
  try:
90
+ response = requests.post(url, headers=headers, json=payload, timeout=10)
91
+ response.raise_for_status()
92
  result = response.json()
93
  return result['choices'][0]['message']['content'].strip()
94
+ except requests.exceptions.HTTPError as e:
95
+ if response.status_code == 503 and attempt < retries - 1:
96
  time.sleep(delay)
97
  continue
98
  else:
99
+ return f"⚠️ Groq API HTTPError: {e}"
100
+ except Exception as e:
101
+ return f"⚠️ Groq API Error: {e}"
102
 
103
  # --- Twilio Chat Handlers ---
104
  def fetch_latest_incoming_message(account_sid, auth_token, conversation_sid):
 
124
  # Load from Hugging Face secrets
125
  account_sid = st.secrets.get("TWILIO_SID")
126
  auth_token = st.secrets.get("TWILIO_TOKEN")
127
+ conversation_sid = "CH5093a87140a147ac926067cca276dbd1"
128
  GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
129
 
130
  # Fallback for testing