Spaces:
Sleeping
Sleeping
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(
|
48 |
|
49 |
# Limit history length to the last 10 messages
|
50 |
chat_history = chat_history[-10:]
|
@@ -56,29 +56,21 @@ 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 |
-
response
|
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 |
-
|
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.
|
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.
|
82 |
|
83 |
# Define the layout and components
|
84 |
generate_button = gr.Button("Generate Response")
|
|
|
8 |
load_dotenv()
|
9 |
|
10 |
# Retrieve API key from environment variable
|
11 |
+
GEMINI_API_KEY = "AIzaSyA0SnGcdEuesDusLiM93N68-vaFF14RCYg" # public api
|
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_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 |
+
response = chat_session.send_message("\n".join(chat_history))
|
60 |
+
return response.text, chat_history
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
except Exception as e:
|
63 |
if attempt < retry_attempts - 1:
|
64 |
time.sleep(2) # Delay before retrying
|
65 |
continue
|
66 |
else:
|
67 |
+
return f"Error after {retry_attempts} attempts: {str(e)}", chat_history
|
|
|
|
|
68 |
|
69 |
# Build the Gradio interface
|
70 |
with gr.Blocks(theme="Hev832/Applio") as iface:
|
71 |
+
chat_input = gr.Textbox(lines=2, label="Talk to AI", placeholder="Enter your message here...")
|
|
|
72 |
chat_history_state = gr.State([]) # State input for chat history
|
73 |
+
response_output = gr.Textbox(label="Response")
|
74 |
|
75 |
# Define the layout and components
|
76 |
generate_button = gr.Button("Generate Response")
|