Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from dotenv import load_dotenv
|
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
# Retrieve API key from environment variable
|
| 9 |
-
GEMINI_API_KEY = "AIzaSyA0SnGcdEuesDusLiM93N68-vaFF14RCYg" # public
|
| 10 |
|
| 11 |
# Configure Google Gemini API
|
| 12 |
genai.configure(api_key=GEMINI_API_KEY)
|
|
@@ -26,23 +26,19 @@ safety_settings = [
|
|
| 26 |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}
|
| 27 |
]
|
| 28 |
|
| 29 |
-
# Function to generate a response based on user input and chat history
|
| 30 |
def generate_response(user_input, chat_history):
|
| 31 |
"""Generates a response based on user input and chat history."""
|
| 32 |
|
| 33 |
-
# Update system content with the full character description
|
| 34 |
-
updated_system_content = "You are Shadow the Hedgehog and you must act like Shadow the Hedgehog's personality."
|
| 35 |
-
|
| 36 |
# Create the generative model
|
| 37 |
model = genai.GenerativeModel(
|
| 38 |
model_name="gemini-1.5-pro",
|
| 39 |
generation_config=generation_config,
|
| 40 |
safety_settings=safety_settings,
|
| 41 |
-
system_instruction=
|
| 42 |
)
|
| 43 |
|
| 44 |
# Add user input to history
|
| 45 |
-
chat_history.append(
|
| 46 |
|
| 47 |
# Limit history length to the last 10 messages
|
| 48 |
chat_history = chat_history[-10:]
|
|
@@ -53,22 +49,25 @@ def generate_response(user_input, chat_history):
|
|
| 53 |
# Start a new chat session
|
| 54 |
chat_session = model.start_chat()
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
return chat_history
|
| 60 |
|
| 61 |
except Exception as e:
|
| 62 |
if attempt < retry_attempts - 1:
|
| 63 |
continue
|
| 64 |
else:
|
| 65 |
-
chat_history.append(
|
| 66 |
return chat_history
|
| 67 |
|
| 68 |
-
# Build the Gradio interface using
|
| 69 |
with gr.Blocks() as iface:
|
| 70 |
chatbot = gr.Chatbot() # Create a Chatbot component
|
| 71 |
-
user_input = gr.Textbox(label="Talk to AI", placeholder="Enter your message here...")
|
| 72 |
chat_history_state = gr.State([]) # State input for chat history
|
| 73 |
|
| 74 |
# Define the layout and components
|
|
|
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
# Retrieve API key from environment variable
|
| 9 |
+
GEMINI_API_KEY = "AIzaSyA0SnGcdEuesDusLiM93N68-vaFF14RCYg" # public API
|
| 10 |
|
| 11 |
# Configure Google Gemini API
|
| 12 |
genai.configure(api_key=GEMINI_API_KEY)
|
|
|
|
| 26 |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}
|
| 27 |
]
|
| 28 |
|
|
|
|
| 29 |
def generate_response(user_input, chat_history):
|
| 30 |
"""Generates a response based on user input and chat history."""
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
# Create the generative model
|
| 33 |
model = genai.GenerativeModel(
|
| 34 |
model_name="gemini-1.5-pro",
|
| 35 |
generation_config=generation_config,
|
| 36 |
safety_settings=safety_settings,
|
| 37 |
+
system_instruction="You are Shadow the Hedgehog and you must act like Shadow the Hedgehog's personality.",
|
| 38 |
)
|
| 39 |
|
| 40 |
# Add user input to history
|
| 41 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 42 |
|
| 43 |
# Limit history length to the last 10 messages
|
| 44 |
chat_history = chat_history[-10:]
|
|
|
|
| 49 |
# Start a new chat session
|
| 50 |
chat_session = model.start_chat()
|
| 51 |
|
| 52 |
+
# Format the history for the model
|
| 53 |
+
formatted_history = "\n".join([f"{entry['role']}: {entry['content']}" for entry in chat_history])
|
| 54 |
+
response = chat_session.send_message(formatted_history)
|
| 55 |
+
|
| 56 |
+
# Append the assistant's response to history
|
| 57 |
+
chat_history.append({"role": "assistant", "content": response.text})
|
| 58 |
return chat_history
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
if attempt < retry_attempts - 1:
|
| 62 |
continue
|
| 63 |
else:
|
| 64 |
+
chat_history.append({"role": "assistant", "content": f"Error after {retry_attempts} attempts: {str(e)}"})
|
| 65 |
return chat_history
|
| 66 |
|
| 67 |
+
# Build the Gradio interface using Chatbot
|
| 68 |
with gr.Blocks() as iface:
|
| 69 |
chatbot = gr.Chatbot() # Create a Chatbot component
|
| 70 |
+
user_input = gr.Textbox(label="Talk to AI", placeholder="Enter your message here...", lines=2)
|
| 71 |
chat_history_state = gr.State([]) # State input for chat history
|
| 72 |
|
| 73 |
# Define the layout and components
|