File size: 979 Bytes
b7c7b2d 92ba5c1 b7c7b2d |
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 |
# chat_app.py
import os
import gradio as gr
import google.generativeai as genai
# 1️⃣ Configure the SDK – read the key from an env-var
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
# 2️⃣ Create a model and a stateful chat session
model = genai.GenerativeModel("gemini-2.0-flash") # change model name if you like
chat = model.start_chat() # history handled for you
# 3️⃣ Callback that Gradio calls each turn
def respond(message, history):
reply = chat.send_message(message) # send the new user turn
return reply.text # only the assistant reply
# 4️⃣ Build the UI
iface = gr.ChatInterface(
fn=respond,
title="Gemini Chatbot",
chatbot=gr.Chatbot(height=600),
textbox=gr.Textbox(placeholder="Ask anything…"),
retry_btn="🔄 Retry",
clear_btn="🗑️ Clear",
)
# 5️⃣ Launch the app
if __name__ == "__main__":
iface.launch()
|