Spaces:
Sleeping
Sleeping
Commit
·
025da1c
1
Parent(s):
0b66c85
app.py
CHANGED
|
@@ -2,35 +2,23 @@ import gradio as gr
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
from dotenv import load_dotenv
|
| 7 |
-
load_dotenv()
|
| 8 |
-
|
| 9 |
-
# Set up Gemini API key (use an environment variable for security)
|
| 10 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 11 |
-
if not api_key:
|
| 12 |
-
print("Warning: GEMINI_API_KEY is not set. Please add it in Hugging Face Space secrets.")
|
| 13 |
-
else:
|
| 14 |
-
print(f"API Key detected, length: {len(api_key)}")
|
| 15 |
-
genai.configure(api_key=api_key)
|
| 16 |
|
| 17 |
def chat_with_ai(prompt):
|
| 18 |
-
"""
|
| 19 |
if not api_key:
|
| 20 |
-
return "Error:
|
| 21 |
try:
|
|
|
|
| 22 |
model = genai.GenerativeModel("gemini-pro")
|
| 23 |
response = model.generate_content(prompt)
|
| 24 |
return response.text
|
| 25 |
except Exception as e:
|
| 26 |
return f"Error: {str(e)}"
|
| 27 |
|
| 28 |
-
# Create Gradio interface
|
| 29 |
-
iface = gr.
|
| 30 |
-
fn=chat_with_ai,
|
| 31 |
-
inputs=gr.Textbox(placeholder="Ask me anything..."),
|
| 32 |
-
outputs=gr.Textbox()
|
| 33 |
-
)
|
| 34 |
|
| 35 |
# Launch the app
|
| 36 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Set up Gemini API key
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
api_key = os.getenv("GEMINI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def chat_with_ai(prompt):
|
| 9 |
+
"""Simple chatbot using Google's Gemini AI."""
|
| 10 |
if not api_key:
|
| 11 |
+
return "Error: API key missing. Set GEMINI_API_KEY in Hugging Face secrets."
|
| 12 |
try:
|
| 13 |
+
genai.configure(api_key=api_key)
|
| 14 |
model = genai.GenerativeModel("gemini-pro")
|
| 15 |
response = model.generate_content(prompt)
|
| 16 |
return response.text
|
| 17 |
except Exception as e:
|
| 18 |
return f"Error: {str(e)}"
|
| 19 |
|
| 20 |
+
# Create Gradio chatbot interface
|
| 21 |
+
iface = gr.ChatInterface(fn=chat_with_ai)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Launch the app
|
| 24 |
if __name__ == "__main__":
|