Spaces:
Runtime error
Runtime error
File size: 1,892 Bytes
f001211 0f2aaf1 722e6c7 f001211 0f2aaf1 f001211 0f2aaf1 722e6c7 f001211 722e6c7 0f2aaf1 722e6c7 f001211 0f2aaf1 f001211 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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()
|