Spaces:
Sleeping
Sleeping
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() | |