Hev832 commited on
Commit
07eb4b6
1 Parent(s): ad0f590

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import gradio as gr
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
 
5
 
6
  # Load environment variables from .env file
7
  load_dotenv()
@@ -21,12 +22,10 @@ generation_config = {
21
  "response_mime_type": "text/plain",
22
  }
23
 
24
- # Define safety settings for the model
25
  safety_settings = [
26
  {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
27
- {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
28
- {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
29
- {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
30
  ]
31
 
32
  # Function to generate a response based on user input and chat history
@@ -50,16 +49,22 @@ def generate_response(user_input, chat_history):
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:
 
2
  import gradio as gr
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
5
+ import time
6
 
7
  # Load environment variables from .env file
8
  load_dotenv()
 
22
  "response_mime_type": "text/plain",
23
  }
24
 
25
+ # Simplified safety settings (or try removing them to test)
26
  safety_settings = [
27
  {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
28
+ {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}
 
 
29
  ]
30
 
31
  # Function to generate a response based on user input and chat history
 
49
  # Limit history length to the last 10 messages
50
  chat_history = chat_history[-10:]
51
 
52
+ retry_attempts = 3
53
+ for attempt in range(retry_attempts):
54
+ try:
55
+ # Start a new chat session
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: