random2222 commited on
Commit
0f2aaf1
·
verified ·
1 Parent(s): 0e9b9d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -54
app.py CHANGED
@@ -1,70 +1,50 @@
1
-
2
  import gradio as gr
3
- import requests
4
  import os
5
 
6
- # Fetch your Groq API key securely from HF Secrets
7
- groq_api_key = os.getenv("GROQ_API_KEY")
 
 
 
8
 
9
- # Path to local business.txt (uploaded in your Space repo)
10
  business_file = os.path.join(os.path.dirname(__file__), "business.txt")
11
 
12
  def chat_with_business(message, history):
13
- try:
14
- # Read business knowledge from local file
15
- with open(business_file, "r", encoding="utf-8") as f:
16
- business_info = f.read().strip()
17
-
18
- # System prompt including business details
19
- system_prompt = (
20
- "You are a helpful customer care assistant. "
21
- "Use only the following business information to answer the user's query:\n\n" +
22
- business_info +
23
- "\n\nIf the answer is not in the knowledge, reply 'Yeh information abhi available nahi hai.'"
24
- )
25
-
26
- # Prepare Groq API payload
27
- headers = {
28
- "Authorization": f"Bearer {groq_api_key}",
29
- "Content-Type": "application/json"
30
- }
31
- payload = {
32
- "model": "llama3-70b-8192",
33
- "messages": [
34
- {"role": "system", "content": system_prompt},
35
- {"role": "user", "content": message}
36
- ],
37
- "temperature": 0.7
38
- }
39
-
40
- # Call Groq chat endpoint
41
- response = requests.post(
42
- "https://api.groq.com/openai/v1/chat/completions",
43
- headers=headers,
44
- json=payload
45
- )
46
- response.raise_for_status()
47
- data = response.json()
48
- reply = data["choices"][0]["message"]["content"]
49
- return reply
50
-
51
- except Exception as e:
52
- return f"Error: {e}"
53
-
54
- # Build Gradio interface
55
  with gr.Blocks(theme="soft") as demo:
56
  gr.Markdown("## 🌿 My Business Bot")
57
  gr.Markdown("*Ask anything about your business in Hindi-English*")
58
  chatbot = gr.Chatbot(elem_id="chatbox", height=400)
59
  user_input = gr.Textbox(placeholder="Type your question here...", show_label=False)
60
 
61
- def handle_interaction(message, chat_history):
62
- bot_reply = chat_with_business(message, chat_history)
63
- chat_history = chat_history + [(message, bot_reply)]
64
- return chat_history, ""
65
-
66
- user_input.submit(handle_interaction, [user_input, chatbot], [chatbot, user_input])
67
 
68
- # Launch app
69
  if __name__ == "__main__":
70
  demo.launch()
 
 
1
  import gradio as gr
2
+ from google import generativeai as genai # Gemini GenAI SDK :contentReference[oaicite:0]{index=0}
3
  import os
4
 
5
+ # Load and configure Gemini API key from HF Secrets
6
+ gemini_api_key = os.getenv("GEMINI_API_KEY")
7
+ if not gemini_api_key:
8
+ raise ValueError("GEMINI_API_KEY not set in environment") # secure secret
9
+ genai.configure(api_key=gemini_api_key)
10
 
11
+ # Path to your uploaded business.txt in the Space
12
  business_file = os.path.join(os.path.dirname(__file__), "business.txt")
13
 
14
  def chat_with_business(message, history):
15
+ # 1️⃣ Read the business knowledge
16
+ with open(business_file, "r", encoding="utf-8") as f:
17
+ business_info = f.read().strip()
18
+
19
+ # 2️⃣ Build the system prompt
20
+ system_prompt = (
21
+ "You are a helpful customer-care assistant. "
22
+ "Use only the information below to answer questions. "
23
+ "If the answer is not present, reply 'Yeh information abhi available nahi hai.'\n\n"
24
+ f"{business_info}\n\n"
25
+ )
26
+
27
+ # 3️⃣ Call Gemini 2.5 Flash to generate response :contentReference[oaicite:1]{index=1}
28
+ model = genai.GenerativeModel(model_name="gemini-2.5-flash-preview-04-17")
29
+ response = model.generate_content(
30
+ system_prompt + "User: " + message
31
+ )
32
+
33
+ # 4️⃣ Return the assistant’s reply
34
+ return response.text
35
+
36
+ # Build Gradio frontend (Blocks API for future customization)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  with gr.Blocks(theme="soft") as demo:
38
  gr.Markdown("## 🌿 My Business Bot")
39
  gr.Markdown("*Ask anything about your business in Hindi-English*")
40
  chatbot = gr.Chatbot(elem_id="chatbox", height=400)
41
  user_input = gr.Textbox(placeholder="Type your question here...", show_label=False)
42
 
43
+ user_input.submit(
44
+ lambda msg, hist: (chat_with_business(msg, hist), ""),
45
+ [user_input, chatbot],
46
+ [chatbot, user_input]
47
+ )
 
48
 
 
49
  if __name__ == "__main__":
50
  demo.launch()