Spaces:
Sleeping
Sleeping
Commit
·
0b66c85
1
Parent(s):
0382b8e
10 billionth update
Browse files- app.py +14 -12
- requirements.txt +3 -2
app.py
CHANGED
|
@@ -1,25 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
if not api_key:
|
| 8 |
-
print("Warning:
|
| 9 |
else:
|
| 10 |
print(f"API Key detected, length: {len(api_key)}")
|
| 11 |
-
|
| 12 |
|
| 13 |
def chat_with_ai(prompt):
|
| 14 |
-
"""Generates a response from
|
| 15 |
if not api_key:
|
| 16 |
return "Error: Missing API key. Please configure it in the Hugging Face Space settings."
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
)
|
| 22 |
-
return response["choices"][0]["message"]["content"]
|
| 23 |
except Exception as e:
|
| 24 |
return f"Error: {str(e)}"
|
| 25 |
|
|
@@ -32,4 +34,4 @@ iface = gr.Interface(
|
|
| 32 |
|
| 33 |
# Launch the app
|
| 34 |
if __name__ == "__main__":
|
| 35 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Load environment variables manually
|
| 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 |
+
"""Generates a response from Google's Gemini AI."""
|
| 19 |
if not api_key:
|
| 20 |
return "Error: Missing API key. Please configure it in the Hugging Face Space settings."
|
| 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 |
|
|
|
|
| 34 |
|
| 35 |
# Launch the app
|
| 36 |
if __name__ == "__main__":
|
| 37 |
+
iface.launch()
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
gradio
|
|
|
|
|
|
| 1 |
+
google-generativeai
|
| 2 |
+
gradio
|
| 3 |
+
python-dotenv
|