Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ load_dotenv()
|
|
| 9 |
# Retrieve API key from environment variable
|
| 10 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 11 |
|
| 12 |
-
|
| 13 |
# Configure Google Gemini API
|
| 14 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 15 |
|
|
@@ -32,11 +31,10 @@ safety_settings = [
|
|
| 32 |
|
| 33 |
# Function to generate a response based on user input and chat history
|
| 34 |
def generate_response(user_input, chat_history):
|
| 35 |
-
"""Generates a response based on user input
|
| 36 |
-
|
| 37 |
|
| 38 |
# Update system content with the full character description
|
| 39 |
-
updated_system_content =
|
| 40 |
|
| 41 |
# Create the generative model
|
| 42 |
model = genai.GenerativeModel(
|
|
@@ -52,35 +50,29 @@ def generate_response(user_input, chat_history):
|
|
| 52 |
# Limit history length to the last 10 messages
|
| 53 |
chat_history = chat_history[-10:]
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# Send the entire chat history as the first message
|
| 59 |
-
response = chat_session.send_message("\n".join(chat_history))
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Build the Gradio interface
|
| 66 |
-
with gr.Blocks(
|
| 67 |
-
|
| 68 |
-
)
|
| 69 |
-
gr.
|
|
|
|
|
|
|
| 70 |
fn=generate_response,
|
| 71 |
-
inputs=[
|
| 72 |
-
|
| 73 |
-
gr.State([]), # State input for chat history
|
| 74 |
-
],
|
| 75 |
-
outputs=[
|
| 76 |
-
gr.Textbox(label="Response"),
|
| 77 |
-
gr.State([]) # State output to update chat history
|
| 78 |
-
],
|
| 79 |
title="Shadow Chat",
|
| 80 |
-
description=
|
| 81 |
-
"<center>Chat with Shadow the Hedgehog!<br>"
|
| 82 |
-
|
| 83 |
-
)
|
| 84 |
)
|
| 85 |
|
| 86 |
-
iface.launch()
|
|
|
|
| 9 |
# Retrieve API key from environment variable
|
| 10 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 11 |
|
|
|
|
| 12 |
# Configure Google Gemini API
|
| 13 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
|
|
|
|
| 31 |
|
| 32 |
# Function to generate a response based on user input and chat history
|
| 33 |
def generate_response(user_input, chat_history):
|
| 34 |
+
"""Generates a response based on user input and chat history."""
|
|
|
|
| 35 |
|
| 36 |
# Update system content with the full character description
|
| 37 |
+
updated_system_content = "You are Shadow the Hedgehog and you must act like Shadow the Hedgehog's personality."
|
| 38 |
|
| 39 |
# Create the generative model
|
| 40 |
model = genai.GenerativeModel(
|
|
|
|
| 50 |
# Limit history length to the last 10 messages
|
| 51 |
chat_history = chat_history[-10:]
|
| 52 |
|
| 53 |
+
try:
|
| 54 |
+
# Start a new chat session
|
| 55 |
+
chat_session = model.start_chat()
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# Send the entire chat history as the first message
|
| 58 |
+
response = chat_session.send_message("\n".join(chat_history))
|
| 59 |
+
return response.text, chat_history
|
| 60 |
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return f"Error: {str(e)}", chat_history
|
| 63 |
|
| 64 |
# Build the Gradio interface
|
| 65 |
+
with gr.Blocks(theme="Hev832/Applio") as iface:
|
| 66 |
+
chat_input = gr.Textbox(lines=2, label="Talk to AI", placeholder="Enter your message here...")
|
| 67 |
+
chat_history_state = gr.State([]) # State input for chat history
|
| 68 |
+
response_output = gr.Textbox(label="Response")
|
| 69 |
+
|
| 70 |
+
iface.interface(
|
| 71 |
fn=generate_response,
|
| 72 |
+
inputs=[chat_input, chat_history_state],
|
| 73 |
+
outputs=[response_output, chat_history_state],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
title="Shadow Chat",
|
| 75 |
+
description="<center>Chat with Shadow the Hedgehog!</center>"
|
|
|
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
+
iface.launch()
|