Spaces:
Sleeping
Sleeping
Commit
·
40212a8
1
Parent(s):
a81180f
- app.py +18 -23
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,34 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Set
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 11 |
-
for user_msg, bot_msg in history:
|
| 12 |
-
messages.append({"role": "user", "content": user_msg})
|
| 13 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 14 |
-
|
| 15 |
-
messages.append({"role": "user", "content": prompt})
|
| 16 |
-
|
| 17 |
-
response = openai.ChatCompletion.create(
|
| 18 |
-
model="gpt-4",
|
| 19 |
-
messages=messages
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# Create
|
| 26 |
chat_interface = gr.ChatInterface(
|
| 27 |
chatbot,
|
| 28 |
-
title="
|
| 29 |
-
description="A chatbot powered by
|
| 30 |
)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
chat_interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Set up Gemini API key (use environment variable on Hugging Face)
|
| 6 |
+
API_KEY = os.getenv("GEMINI_API_KEY") # Set this in Hugging Face secrets
|
| 7 |
+
genai.configure(api_key=API_KEY)
|
| 8 |
|
| 9 |
+
# Use Gemini 1.5 Flash (free-tier accessible)
|
| 10 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def chatbot(prompt, history=[]):
|
| 13 |
+
"""Generates a chatbot response using the free-tier Gemini API."""
|
| 14 |
+
try:
|
| 15 |
+
response = model.generate_content(prompt)
|
| 16 |
+
return response.text
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"Error: {e}"
|
| 19 |
|
| 20 |
+
# Create Gradio chatbot interface
|
| 21 |
chat_interface = gr.ChatInterface(
|
| 22 |
chatbot,
|
| 23 |
+
title="Free Gemini API Chatbot",
|
| 24 |
+
description="A chatbot powered by the free-tier Google Gemini API.",
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# Run the chatbot
|
| 28 |
if __name__ == "__main__":
|
| 29 |
+
chat_interface.launch()
|
requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
|
| 2 |
gradio
|
|
|
|
| 1 |
+
google-generativeai
|
| 2 |
gradio
|