random2222 commited on
Commit
7fdd8cf
·
verified ·
1 Parent(s): cc10aa4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -1,22 +1,22 @@
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. "
@@ -24,27 +24,30 @@ def chat_with_business(message, history):
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()
 
1
  import gradio as gr
2
+ from google import generativeai as genai
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")
9
+ genai.configure(api_key=gemini_api_key) # sets auth for all calls :contentReference[oaicite:2]{index=2}
10
 
11
+ # — Path to your uploaded business.txt
12
  business_file = os.path.join(os.path.dirname(__file__), "business.txt")
13
 
14
  def chat_with_business(message, history):
15
+ # 1️⃣ Read business info
16
  with open(business_file, "r", encoding="utf-8") as f:
17
  business_info = f.read().strip()
18
 
19
+ # 2️⃣ Build system prompt
20
  system_prompt = (
21
  "You are a helpful customer-care assistant. "
22
  "Use only the information below to answer questions. "
 
24
  f"{business_info}\n\n"
25
  )
26
 
27
+ # 3️⃣ Call Gemini 2.5 Flash
28
  model = genai.GenerativeModel(model_name="gemini-2.5-flash-preview-04-17")
29
+ response = model.generate_content(system_prompt + "User: " + message) # :contentReference[oaicite:3]{index=3}
 
 
30
 
31
+ # 4️⃣ Return text
32
  return response.text
33
 
34
+ # — Build Gradio UI with Blocks and messages format
35
  with gr.Blocks(theme="soft") as demo:
36
  gr.Markdown("## 🌿 My Business Bot")
37
  gr.Markdown("*Ask anything about your business in Hindi-English*")
38
+ chatbot = gr.Chatbot(elem_id="chatbox", height=400, type="messages") # use messages format :contentReference[oaicite:4]{index=4}
39
  user_input = gr.Textbox(placeholder="Type your question here...", show_label=False)
40
 
41
+ def handle(msg, hist):
42
+ reply = chat_with_business(msg, hist)
43
+ # Append OpenAI-style dicts, not tuples :contentReference[oaicite:5]{index=5}
44
+ hist = hist + [
45
+ {"role": "user", "content": msg},
46
+ {"role": "assistant", "content": reply}
47
+ ]
48
+ return hist, ""
49
+
50
+ user_input.submit(handle, [user_input, chatbot], [chatbot, user_input])
51
 
52
  if __name__ == "__main__":
53
  demo.launch()