duatanzeel commited on
Commit
e6a16ed
Β·
verified Β·
1 Parent(s): 36c2986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -29,21 +29,23 @@ def chat(user_input, history):
29
  # Combine knowledge base + chat history + current input
30
  context_text = f"Use the following knowledge base to answer the user's question:\n\n{knowledge_base}\n\n"
31
  context_text += "Previous chat history:\n"
32
- for user_msg, bot_msg in history:
33
- context_text += f"User: {user_msg}\nBot: {bot_msg}\n"
34
  context_text += f"User: {user_input}\nAnswer based on the knowledge above."
35
 
36
- # Send everything to Gemini (as string)
37
  try:
38
  client = genai.Client(api_key=global_api_key)
39
  chat_session = client.chats.create(model="gemini-2.5-flash")
40
- response = chat_session.send_message(context_text) # send as string
41
  reply = response.text
42
  except Exception as e:
43
  reply = f"❌ Error calling Gemini model: {e}"
44
 
45
- history.append((user_input, reply)) # store chat history
46
- return history, ""
 
 
 
47
 
48
  # --- Gradio UI ---
49
  with gr.Blocks() as demo:
@@ -54,15 +56,17 @@ with gr.Blocks() as demo:
54
  set_key_btn = gr.Button("Set API Key")
55
  api_status = gr.Textbox(label="Status", interactive=False)
56
 
 
 
 
 
 
57
  # Chatbot interface
58
- chatbot = gr.Chatbot()
59
  user_input = gr.Textbox(label="Ask a question")
60
  send_btn = gr.Button("Send")
61
  clear_btn = gr.Button("Clear Chat")
62
 
63
- # Store chat history in session
64
- chat_history = gr.State([])
65
-
66
  # Set API key
67
  set_key_btn.click(fn=set_api_key, inputs=api_key, outputs=api_status)
68
 
@@ -71,12 +75,6 @@ with gr.Blocks() as demo:
71
  user_input.submit(fn=chat, inputs=[user_input, chat_history], outputs=[chatbot, user_input])
72
 
73
  # Clear chat
74
- clear_btn.click(lambda: [], None, chatbot)
75
-
76
- # Initial greeting message
77
- def greeting():
78
- return [("System", "πŸ‘‹ Welcome to Arduino Expert! Ask me anything about Arduino projects.")]
79
-
80
- chatbot.update(greeting())
81
 
82
  demo.launch(share=True)
 
29
  # Combine knowledge base + chat history + current input
30
  context_text = f"Use the following knowledge base to answer the user's question:\n\n{knowledge_base}\n\n"
31
  context_text += "Previous chat history:\n"
32
+ for msg in history:
33
+ context_text += f"{msg['role'].capitalize()}: {msg['content']}\n"
34
  context_text += f"User: {user_input}\nAnswer based on the knowledge above."
35
 
 
36
  try:
37
  client = genai.Client(api_key=global_api_key)
38
  chat_session = client.chats.create(model="gemini-2.5-flash")
39
+ response = chat_session.send_message(context_text)
40
  reply = response.text
41
  except Exception as e:
42
  reply = f"❌ Error calling Gemini model: {e}"
43
 
44
+ # Append messages in new Gradio format
45
+ history.append({"role": "user", "content": user_input})
46
+ history.append({"role": "assistant", "content": reply})
47
+
48
+ return history, "" # "" clears the user input box
49
 
50
  # --- Gradio UI ---
51
  with gr.Blocks() as demo:
 
56
  set_key_btn = gr.Button("Set API Key")
57
  api_status = gr.Textbox(label="Status", interactive=False)
58
 
59
+ # Store chat history in session with initial greeting
60
+ chat_history = gr.State([
61
+ {"role": "system", "content": "πŸ‘‹ Welcome to Arduino Expert! Ask me anything about Arduino projects."}
62
+ ])
63
+
64
  # Chatbot interface
65
+ chatbot = gr.Chatbot(type="messages")
66
  user_input = gr.Textbox(label="Ask a question")
67
  send_btn = gr.Button("Send")
68
  clear_btn = gr.Button("Clear Chat")
69
 
 
 
 
70
  # Set API key
71
  set_key_btn.click(fn=set_api_key, inputs=api_key, outputs=api_status)
72
 
 
75
  user_input.submit(fn=chat, inputs=[user_input, chat_history], outputs=[chatbot, user_input])
76
 
77
  # Clear chat
78
+ clear_btn.click(lambda: [{"role": "system", "content": "πŸ‘‹ Welcome to Arduino Expert! Ask me anything about Arduino projects."}], None, chatbot)
 
 
 
 
 
 
79
 
80
  demo.launch(share=True)