Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import time
|
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
# Retrieve API key from environment variable
|
| 11 |
-
GEMINI_API_KEY = "
|
| 12 |
|
| 13 |
# Configure Google Gemini API
|
| 14 |
genai.configure(api_key=GEMINI_API_KEY)
|
|
@@ -44,7 +44,7 @@ def generate_response(user_input, chat_history):
|
|
| 44 |
)
|
| 45 |
|
| 46 |
# Add user input to history
|
| 47 |
-
chat_history.append(user_input)
|
| 48 |
|
| 49 |
# Limit history length to the last 10 messages
|
| 50 |
chat_history = chat_history[-10:]
|
|
@@ -56,20 +56,27 @@ def generate_response(user_input, chat_history):
|
|
| 56 |
chat_session = model.start_chat()
|
| 57 |
|
| 58 |
# Send the entire chat history as the first message
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
if attempt < retry_attempts - 1:
|
| 64 |
time.sleep(2) # Delay before retrying
|
| 65 |
continue
|
| 66 |
else:
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Build the Gradio interface
|
| 70 |
with gr.Blocks(theme="Hev832/Applio") as iface:
|
| 71 |
-
gr.Markdown("<center><h1>Chat with Shadow The Hedgehog")
|
| 72 |
-
chat_input = gr.Textbox(lines=2, label="Talk to
|
| 73 |
chat_history_state = gr.State([]) # State input for chat history
|
| 74 |
response_output = gr.Chatbot(label="Response")
|
| 75 |
|
|
|
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
# Retrieve API key from environment variable
|
| 11 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # Securely retrieve the API key from the environment
|
| 12 |
|
| 13 |
# Configure Google Gemini API
|
| 14 |
genai.configure(api_key=GEMINI_API_KEY)
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
# Add user input to history
|
| 47 |
+
chat_history.append(("User", user_input))
|
| 48 |
|
| 49 |
# Limit history length to the last 10 messages
|
| 50 |
chat_history = chat_history[-10:]
|
|
|
|
| 56 |
chat_session = model.start_chat()
|
| 57 |
|
| 58 |
# Send the entire chat history as the first message
|
| 59 |
+
messages = [f"{sender}: {message}" for sender, message in chat_history]
|
| 60 |
+
response = chat_session.send_message("\n".join(messages))
|
| 61 |
+
|
| 62 |
+
# Add the response to the chat history
|
| 63 |
+
chat_history.append(("Shadow", response.text))
|
| 64 |
+
|
| 65 |
+
return chat_history, chat_history
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
if attempt < retry_attempts - 1:
|
| 69 |
time.sleep(2) # Delay before retrying
|
| 70 |
continue
|
| 71 |
else:
|
| 72 |
+
error_message = f"Error after {retry_attempts} attempts: {str(e)}"
|
| 73 |
+
chat_history.append(("Shadow", error_message))
|
| 74 |
+
return chat_history, chat_history
|
| 75 |
|
| 76 |
# Build the Gradio interface
|
| 77 |
with gr.Blocks(theme="Hev832/Applio") as iface:
|
| 78 |
+
gr.Markdown("<center><h1>Chat with Shadow The Hedgehog</h1></center>")
|
| 79 |
+
chat_input = gr.Textbox(lines=2, label="Talk to Shadow", placeholder="Enter your message here...")
|
| 80 |
chat_history_state = gr.State([]) # State input for chat history
|
| 81 |
response_output = gr.Chatbot(label="Response")
|
| 82 |
|