duatanzeel commited on
Commit
366e523
Β·
verified Β·
1 Parent(s): c40f232

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -8,7 +8,7 @@ from google.genai import types
8
  MODEL_NAME = "gemini-2.5-flash"
9
  embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
10
 
11
- # βœ… Load knowledge base from file (you create knowledge.txt in the repo)
12
  with open("knowledge.txt", "r", encoding="utf-8") as f:
13
  raw_text = f.read()
14
 
@@ -39,7 +39,6 @@ If the answer is not in the context, reply:
39
  """
40
  USER_TEMPLATE = """Client asked:
41
  {question}
42
-
43
  Relevant chat context:
44
  {context}
45
  """
@@ -54,7 +53,7 @@ def retrieve(query, top_k=3):
54
  results.append(meta[i]["chunk"])
55
  return results
56
 
57
- # βœ… Call Gemini (with user-provided API key)
58
  def call_gemini(api_key, system_prompt, user_prompt):
59
  try:
60
  client = genai.Client(api_key=api_key)
@@ -80,24 +79,32 @@ def answer_question(api_key, question, top_k=3):
80
  user_prompt = USER_TEMPLATE.format(question=question, context=context)
81
  return call_gemini(api_key, SYSTEM_TEMPLATE, user_prompt)
82
 
83
- # βœ… Gradio Chatbot
84
- def chatbot_fn(message, history, api_key):
85
- reply = answer_question(api_key, message)
86
- return reply
87
-
88
  with gr.Blocks() as demo:
89
  gr.Markdown("## πŸ€– Arduino Fiverr Chatbot (Gemini Powered)")
90
 
91
- with gr.Row():
92
- api_key_box = gr.Textbox(
93
- label="πŸ”‘ Enter your Gemini API Key",
94
- placeholder="Paste your Gemini API key here...",
95
- type="password",
96
- )
97
-
98
- gr.ChatInterface(
99
- fn=lambda message, history: chatbot_fn(message, history, api_key_box.value),
100
- title="Arduino Client Assistant"
101
  )
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  demo.launch()
 
8
  MODEL_NAME = "gemini-2.5-flash"
9
  embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
10
 
11
+ # βœ… Load knowledge base from file (put your content inside knowledge.txt in repo)
12
  with open("knowledge.txt", "r", encoding="utf-8") as f:
13
  raw_text = f.read()
14
 
 
39
  """
40
  USER_TEMPLATE = """Client asked:
41
  {question}
 
42
  Relevant chat context:
43
  {context}
44
  """
 
53
  results.append(meta[i]["chunk"])
54
  return results
55
 
56
+ # βœ… Call Gemini
57
  def call_gemini(api_key, system_prompt, user_prompt):
58
  try:
59
  client = genai.Client(api_key=api_key)
 
79
  user_prompt = USER_TEMPLATE.format(question=question, context=context)
80
  return call_gemini(api_key, SYSTEM_TEMPLATE, user_prompt)
81
 
82
+ # βœ… Gradio App
 
 
 
 
83
  with gr.Blocks() as demo:
84
  gr.Markdown("## πŸ€– Arduino Fiverr Chatbot (Gemini Powered)")
85
 
86
+ api_key_box = gr.Textbox(
87
+ label="πŸ”‘ Enter your Gemini API Key",
88
+ placeholder="Paste your Gemini API key here...",
89
+ type="password",
 
 
 
 
 
 
90
  )
91
 
92
+ chatbot = gr.Chatbot()
93
+ msg = gr.Textbox(placeholder="Ask your Arduino client question here...")
94
+ state = gr.State("")
95
+
96
+ def save_key(api_key):
97
+ return api_key, "βœ… API Key saved! You can now ask questions."
98
+
99
+ def chatbot_fn(message, history, api_key):
100
+ reply = answer_question(api_key, message)
101
+ history.append((message, reply))
102
+ return history, ""
103
+
104
+ # Save API key
105
+ api_key_box.change(save_key, inputs=[api_key_box], outputs=[state, chatbot])
106
+
107
+ # Chat flow
108
+ msg.submit(chatbot_fn, inputs=[msg, chatbot, state], outputs=[chatbot, msg])
109
+
110
  demo.launch()