|
|
import gradio as gr |
|
|
import random |
|
|
|
|
|
|
|
|
chat_history = [] |
|
|
|
|
|
|
|
|
knowledge_base = { |
|
|
"battery": [ |
|
|
"Batteries store chemical energy and convert it into electrical energy.", |
|
|
"Lithium-ion batteries are the most common type used in EVs and smartphones.", |
|
|
"Battery performance depends on temperature, charge cycles, and chemistry." |
|
|
], |
|
|
"lithium": [ |
|
|
"Lithium is a lightweight metal used in rechargeable batteries.", |
|
|
"Lithium-ion technology offers high energy density and long cycle life." |
|
|
], |
|
|
"ev": [ |
|
|
"Electric vehicles (EVs) use electric motors powered by batteries.", |
|
|
"EVs help reduce carbon emissions and depend on efficient battery systems." |
|
|
], |
|
|
"renewable": [ |
|
|
"Renewable energy includes solar, wind, hydro, and geothermal sources.", |
|
|
"Sustainable energy storage is key to renewable adoption." |
|
|
], |
|
|
"default": [ |
|
|
"I'm VoltAIon ⚡, your AI research assistant for sustainable energy and battery materials!", |
|
|
"Could you please clarify your question? I’ll do my best to help!", |
|
|
"That’s an interesting topic! Can you tell me more about it?" |
|
|
] |
|
|
} |
|
|
|
|
|
def local_ai_response(user_input): |
|
|
"""Generate a response without any API (simple keyword-based logic).""" |
|
|
global chat_history |
|
|
|
|
|
|
|
|
text = user_input.lower() |
|
|
|
|
|
|
|
|
for keyword in knowledge_base.keys(): |
|
|
if keyword in text: |
|
|
bot_reply = random.choice(knowledge_base[keyword]) |
|
|
break |
|
|
else: |
|
|
bot_reply = random.choice(knowledge_base["default"]) |
|
|
|
|
|
|
|
|
chat_history.append(("user", user_input)) |
|
|
chat_history.append(("assistant", bot_reply)) |
|
|
return chat_history |
|
|
|
|
|
|
|
|
def reset_chat(): |
|
|
"""Clear chat history.""" |
|
|
global chat_history |
|
|
chat_history = [] |
|
|
return [] |
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green", secondary_hue="emerald")) as demo: |
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
gr.Markdown( |
|
|
""" |
|
|
# ⚡ VoltAIon - AI Research Assistant |
|
|
🤖 *Your Sustainable Energy & Battery Materials Expert* |
|
|
--- |
|
|
Ask any question about **battery chemistry, EV storage, lithium-ion, or renewable energy solutions.** |
|
|
""" |
|
|
) |
|
|
clear_btn = gr.Button("🔄 Reset Chat", variant="secondary") |
|
|
|
|
|
with gr.Column(scale=2): |
|
|
chatbot = gr.Chatbot(label="VoltAIon Chat", height=500, bubble_full_width=False, show_copy_button=True) |
|
|
user_input = gr.Textbox(placeholder="Type your question here...", label="Your Question") |
|
|
submit_btn = gr.Button("🚀 Ask AI", variant="primary") |
|
|
|
|
|
submit_btn.click(local_ai_response, inputs=user_input, outputs=chatbot) |
|
|
user_input.submit(local_ai_response, inputs=user_input, outputs=chatbot) |
|
|
clear_btn.click(reset_chat, outputs=chatbot) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|