Spaces:
Sleeping
Sleeping
prithivMLmods
commited on
Commit
•
5558c42
1
Parent(s):
8312a74
Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,7 @@ footer {
|
|
14 |
|
15 |
load_dotenv()
|
16 |
|
17 |
-
GEMINI_API_KEY = "
|
18 |
|
19 |
genai.configure(api_key=GEMINI_API_KEY)
|
20 |
|
@@ -22,20 +22,19 @@ generation_config = {
|
|
22 |
"temperature": 0.7,
|
23 |
"top_p": 0.95,
|
24 |
"top_k": 64,
|
25 |
-
"max_output_tokens": 512,
|
26 |
"response_mime_type": "text/plain",
|
27 |
}
|
28 |
|
29 |
-
safety_settings = [
|
30 |
-
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
31 |
-
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}
|
32 |
-
]
|
33 |
-
|
34 |
mood_prompts = {
|
35 |
"Fun": "Respond in a light-hearted, playful manner.",
|
36 |
"Serious": "Respond in a thoughtful, serious tone.",
|
37 |
"Professional": "Respond in a formal, professional manner.",
|
38 |
-
"Upset": "Respond in a slightly irritated, upset tone."
|
|
|
|
|
|
|
|
|
39 |
}
|
40 |
|
41 |
def generate_response(user_input, chat_history, mood):
|
@@ -45,34 +44,27 @@ def generate_response(user_input, chat_history, mood):
|
|
45 |
model = genai.GenerativeModel(
|
46 |
model_name="gemini-1.5-pro",
|
47 |
generation_config=generation_config,
|
48 |
-
safety_settings=safety_settings,
|
49 |
system_instruction=updated_system_content,
|
50 |
)
|
51 |
|
52 |
-
# Add user input to history
|
53 |
chat_history.append(user_input)
|
54 |
|
55 |
-
# Limit history length to the last 10 messages
|
56 |
chat_history = chat_history[-10:]
|
57 |
|
58 |
-
retry_attempts =
|
59 |
for attempt in range(retry_attempts):
|
60 |
try:
|
61 |
-
# Start a new chat session
|
62 |
chat_session = model.start_chat()
|
63 |
-
|
64 |
-
# Send the entire chat history as the first message
|
65 |
response = chat_session.send_message("\n".join(chat_history))
|
66 |
return response.text, chat_history
|
67 |
|
68 |
except Exception as e:
|
69 |
if attempt < retry_attempts - 1:
|
70 |
-
time.sleep(2)
|
71 |
continue
|
72 |
else:
|
73 |
return f"Error after {retry_attempts} attempts: {str(e)}", chat_history
|
74 |
|
75 |
-
# Build the Gradio interface
|
76 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as iface:
|
77 |
|
78 |
chat_input = gr.Textbox(lines=2, label="Chatbot", placeholder="Enter your message here...")
|
|
|
14 |
|
15 |
load_dotenv()
|
16 |
|
17 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
18 |
|
19 |
genai.configure(api_key=GEMINI_API_KEY)
|
20 |
|
|
|
22 |
"temperature": 0.7,
|
23 |
"top_p": 0.95,
|
24 |
"top_k": 64,
|
25 |
+
"max_output_tokens": 512,
|
26 |
"response_mime_type": "text/plain",
|
27 |
}
|
28 |
|
|
|
|
|
|
|
|
|
|
|
29 |
mood_prompts = {
|
30 |
"Fun": "Respond in a light-hearted, playful manner.",
|
31 |
"Serious": "Respond in a thoughtful, serious tone.",
|
32 |
"Professional": "Respond in a formal, professional manner.",
|
33 |
+
"Upset": "Respond in a slightly irritated, upset tone.",
|
34 |
+
"Empathetic": "Respond in a warm and understanding tone.",
|
35 |
+
"Optimistic": "Respond in a positive, hopeful manner.",
|
36 |
+
"Sarcastic": "Respond with a hint of sarcasm.",
|
37 |
+
"Motivational": "Respond with encouragement and motivation."
|
38 |
}
|
39 |
|
40 |
def generate_response(user_input, chat_history, mood):
|
|
|
44 |
model = genai.GenerativeModel(
|
45 |
model_name="gemini-1.5-pro",
|
46 |
generation_config=generation_config,
|
|
|
47 |
system_instruction=updated_system_content,
|
48 |
)
|
49 |
|
|
|
50 |
chat_history.append(user_input)
|
51 |
|
|
|
52 |
chat_history = chat_history[-10:]
|
53 |
|
54 |
+
retry_attempts = 10
|
55 |
for attempt in range(retry_attempts):
|
56 |
try:
|
|
|
57 |
chat_session = model.start_chat()
|
|
|
|
|
58 |
response = chat_session.send_message("\n".join(chat_history))
|
59 |
return response.text, chat_history
|
60 |
|
61 |
except Exception as e:
|
62 |
if attempt < retry_attempts - 1:
|
63 |
+
time.sleep(2)
|
64 |
continue
|
65 |
else:
|
66 |
return f"Error after {retry_attempts} attempts: {str(e)}", chat_history
|
67 |
|
|
|
68 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as iface:
|
69 |
|
70 |
chat_input = gr.Textbox(lines=2, label="Chatbot", placeholder="Enter your message here...")
|