Spaces:
Sleeping
Sleeping
File size: 3,977 Bytes
1871256 8c52991 f61173e 5a67d17 ae322d1 69895cd ae322d1 5a67d17 ae322d1 36c2986 5a67d17 5e892d8 69895cd 5d4ff35 69895cd 36c2986 69895cd f89be03 36c2986 f89be03 5d4ff35 ebbbbfe e6a16ed ebbbbfe 5d4ff35 ebbbbfe e6a16ed ebbbbfe 36c2986 f89be03 ecff06c e6a16ed 5a67d17 366e523 ecff06c 3e16187 ecff06c 3e16187 ecff06c 3e16187 ecff06c 3e16187 ecff06c 3e16187 ecff06c 3e16187 ecff06c 3e16187 ecff06c 3e16187 ecff06c 3e16187 ecff06c 36c2986 366e523 ecff06c e6a16ed ebbbbfe ecff06c 3e16187 ecff06c 3e16187 ecff06c 5e892d8 ecff06c e6a16ed 36c2986 5a67d17 |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
import gradio as gr
from google import genai
from google.genai import types
# --- Load knowledge base from repo at startup ---
with open("knowledge.txt", "r", encoding="utf-8") as f:
knowledge_base = f.read()
# --- Store API key globally ---
global_api_key = None
# --- Load Gemini API key from user ---
def set_api_key(api_key):
global global_api_key
global_api_key = api_key.strip()
return "β
API Key set successfully!"
# --- Chat function ---
def chat(user_input, history):
global knowledge_base, global_api_key
history = history or []
if not global_api_key:
return history, "β οΈ Please set your Gemini API key first."
if not knowledge_base:
return history, "β οΈ Knowledge base is empty."
# Combine knowledge base + chat history + current input
context_text = f"Use the following knowledge base to answer the user's question:\n\n{knowledge_base}\n\n"
context_text += "Previous chat history:\n"
for msg in history:
context_text += f"{msg['role'].capitalize()}: {msg['content']}\n"
context_text += f"User: {user_input}\nAnswer based on the knowledge above."
try:
client = genai.Client(api_key=global_api_key)
chat_session = client.chats.create(model="gemini-2.5-flash")
response = chat_session.send_message(context_text)
reply = response.text
except Exception as e:
reply = f"β Error calling Gemini model: {e}"
# Append messages in new Gradio format
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": reply})
return history, "" # "" clears the user input box
# --- Custom CSS for Arduino Expert Theme ---
custom_css = """
.gradio-container {
background-color: #000000 !important; /* Black background */
font-family: 'Arial', sans-serif;
color: white;
}
/* Header */
h1, h2, h3, .gr-markdown {
color: #0082C9 !important; /* Arduino Blue */
text-align: center;
font-weight: bold;
}
/* Buttons */
button {
background-color: #0082C9 !important;
color: white !important;
border-radius: 8px !important;
font-weight: bold !important;
border: none !important;
}
button:hover {
background-color: #006fa8 !important;
}
/* Textboxes */
textarea, input {
border: 2px solid #0082C9 !important;
border-radius: 6px !important;
background-color: #1a1a1a !important;
color: white !important;
}
/* Chatbot */
.gr-chatbot {
border: 2px solid #0082C9 !important;
border-radius: 10px !important;
background-color: #ffffff !important; /* White chat background */
color: black !important;
max-height: 70vh !important;
overflow-y: auto !important;
}
"""
# --- Gradio UI ---
with gr.Blocks(css=custom_css) as demo:
gr.Markdown("## π€ Arduino Expert Chatbot")
# API key input
api_key = gr.Textbox(label="Enter your Gemini API Key", type="password")
set_key_btn = gr.Button("Set API Key")
api_status = gr.Textbox(label="Status", interactive=False)
# Store chat history in session with initial greeting
chat_history = gr.State([
{"role": "system", "content": "π Welcome to Arduino Expert! Ask me anything about Arduino projects."}
])
# Chatbot interface
chatbot = gr.Chatbot(type="messages", label="Conversation")
user_input = gr.Textbox(label="Ask a question")
send_btn = gr.Button("Send")
clear_btn = gr.Button("Clear Chat")
# Set API key
set_key_btn.click(fn=set_api_key, inputs=api_key, outputs=api_status)
# Send message
send_btn.click(fn=chat, inputs=[user_input, chat_history], outputs=[chatbot, user_input])
user_input.submit(fn=chat, inputs=[user_input, chat_history], outputs=[chatbot, user_input])
# Clear chat
clear_btn.click(lambda: [{"role": "system", "content": "π Welcome to Arduino Expert! Ask me anything about Arduino projects."}], None, chatbot)
demo.launch(share=True)
|