File size: 853 Bytes
90ba62d
0b66c85
90ba62d
af14490
025da1c
0b66c85
90ba62d
 
025da1c
0e66e48
025da1c
90ba62d
025da1c
6d3b2b6
 
 
0b66c85
 
90ba62d
 
 
025da1c
 
90ba62d
 
 
0b66c85
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
import gradio as gr
import google.generativeai as genai
import os

# Set up Gemini API key
api_key = os.getenv("GEMINI_API_KEY")

def chat_with_ai(prompt):
    """Simple chatbot using Google's Gemini AI."""
    if not api_key:
        return "Error: API key missing. Set GEMINI_API_KEY in Hugging Face secrets."
    try:
        genai.configure(api_key=api_key)
        available_models = [m.name for m in genai.list_models()]
        model_name = "gemini-pro" if "gemini-pro" in available_models else available_models[0]
        model = genai.GenerativeModel(model_name)
        response = model.generate_content(prompt)
        return response.text
    except Exception as e:
        return f"Error: {str(e)}"

# Create Gradio chatbot interface
iface = gr.ChatInterface(fn=chat_with_ai)

# Launch the app
if __name__ == "__main__":
    iface.launch()