Spaces:
Sleeping
Sleeping
import gradio as gr | |
import google.generativeai as genai | |
import os | |
# Load environment variables manually | |
from dotenv import load_dotenv | |
load_dotenv() | |
# Set up Gemini API key (use an environment variable for security) | |
api_key = os.getenv("GEMINI_API_KEY") | |
if not api_key: | |
print("Warning: GEMINI_API_KEY is not set. Please add it in Hugging Face Space secrets.") | |
else: | |
print(f"API Key detected, length: {len(api_key)}") | |
genai.configure(api_key=api_key) | |
def chat_with_ai(prompt): | |
"""Generates a response from Google's Gemini AI.""" | |
if not api_key: | |
return "Error: Missing API key. Please configure it in the Hugging Face Space settings." | |
try: | |
model = genai.GenerativeModel("gemini-pro") | |
response = model.generate_content(prompt) | |
return response.text | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=chat_with_ai, | |
inputs=gr.Textbox(placeholder="Ask me anything..."), | |
outputs=gr.Textbox() | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
iface.launch() | |