Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,50 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
import os
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
-
# Path to
|
10 |
business_file = os.path.join(os.path.dirname(__file__), "business.txt")
|
11 |
|
12 |
def chat_with_business(message, history):
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
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 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
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()
|