Spaces:
Sleeping
Sleeping
File size: 1,083 Bytes
90ba62d 0b66c85 90ba62d af14490 0b66c85 0e66e48 0b66c85 0e66e48 af14490 0b66c85 90ba62d 0b66c85 0e66e48 90ba62d 0b66c85 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 29 30 31 32 33 34 35 36 37 38 |
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()
|