File size: 856 Bytes
90ba62d
40212a8
90ba62d
af14490
40212a8
 
 
a81180f
7ba8354
 
90ba62d
40212a8
 
 
 
 
 
 
90ba62d
40212a8
a81180f
 
40212a8
 
a81180f
90ba62d
40212a8
90ba62d
40212a8
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
import gradio as gr
import google.generativeai as genai
import os

# Set up Gemini API key (use environment variable on Hugging Face)
API_KEY = os.getenv("GEMINI_API_KEY")  # Set this in Hugging Face secrets
genai.configure(api_key=API_KEY)

# Use Gemini 2.0 Flash (free-tier accessible)
model = genai.GenerativeModel("gemini-2.0-flash")

def chatbot(prompt, history=[]):
    """Generates a chatbot response using the free-tier Gemini API."""
    try:
        response = model.generate_content(prompt)
        return response.text
    except Exception as e:
        return f"Error: {e}"

# Create Gradio chatbot interface
chat_interface = gr.ChatInterface(
    chatbot,
    title="Free Gemini API Chatbot",
    description="A chatbot powered by the free-tier Google Gemini API.",
)

# Run the chatbot
if __name__ == "__main__":
    chat_interface.launch()