Spaces:
Runtime error
Runtime error
import gradio as gr | |
from google import generativeai as genai # Gemini GenAI SDK :contentReference[oaicite:0]{index=0} | |
import os | |
# — Load and configure Gemini API key from HF Secrets | |
gemini_api_key = os.getenv("GEMINI_API_KEY") | |
if not gemini_api_key: | |
raise ValueError("GEMINI_API_KEY not set in environment") # secure secret | |
genai.configure(api_key=gemini_api_key) | |
# — Path to your uploaded business.txt in the Space | |
business_file = os.path.join(os.path.dirname(__file__), "business.txt") | |
def chat_with_business(message, history): | |
# 1️⃣ Read the business knowledge | |
with open(business_file, "r", encoding="utf-8") as f: | |
business_info = f.read().strip() | |
# 2️⃣ Build the system prompt | |
system_prompt = ( | |
"You are a helpful customer-care assistant. " | |
"Use only the information below to answer questions. " | |
"If the answer is not present, reply 'Yeh information abhi available nahi hai.'\n\n" | |
f"{business_info}\n\n" | |
) | |
# 3️⃣ Call Gemini 2.5 Flash to generate response :contentReference[oaicite:1]{index=1} | |
model = genai.GenerativeModel(model_name="gemini-2.5-flash-preview-04-17") | |
response = model.generate_content( | |
system_prompt + "User: " + message | |
) | |
# 4️⃣ Return the assistant’s reply | |
return response.text | |
# — Build Gradio frontend (Blocks API for future customization) | |
with gr.Blocks(theme="soft") as demo: | |
gr.Markdown("## 🌿 My Business Bot") | |
gr.Markdown("*Ask anything about your business in Hindi-English*") | |
chatbot = gr.Chatbot(elem_id="chatbox", height=400) | |
user_input = gr.Textbox(placeholder="Type your question here...", show_label=False) | |
user_input.submit( | |
lambda msg, hist: (chat_with_business(msg, hist), ""), | |
[user_input, chatbot], | |
[chatbot, user_input] | |
) | |
if __name__ == "__main__": | |
demo.launch() | |