Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ from werkzeug.utils import secure_filename
|
|
8 |
import tempfile
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
-
app.secret_key = os.urandom(24)
|
12 |
load_dotenv()
|
13 |
|
14 |
# Configure Google AI
|
@@ -34,6 +34,9 @@ model = genai.GenerativeModel('gemini-2.0-flash-exp',
|
|
34 |
safety_settings=safety_settings,
|
35 |
system_instruction=SYSTEM_PROMPT)
|
36 |
|
|
|
|
|
|
|
37 |
def perform_web_search(query):
|
38 |
conn = http.client.HTTPSConnection("google.serper.dev")
|
39 |
payload = json.dumps({"q": query})
|
@@ -76,14 +79,6 @@ def format_search_results(data):
|
|
76 |
'link': item['link']
|
77 |
})
|
78 |
|
79 |
-
if 'peopleAlsoAsk' in data:
|
80 |
-
for item in data['peopleAlsoAsk'][:2]:
|
81 |
-
result.append({
|
82 |
-
'type': 'question',
|
83 |
-
'question': item['question'],
|
84 |
-
'answer': item['snippet']
|
85 |
-
})
|
86 |
-
|
87 |
return result
|
88 |
|
89 |
UPLOAD_FOLDER = 'temp'
|
@@ -96,9 +91,11 @@ def allowed_file(filename):
|
|
96 |
|
97 |
@app.route('/')
|
98 |
def home():
|
99 |
-
if '
|
100 |
-
session['
|
101 |
-
|
|
|
|
|
102 |
|
103 |
@app.route('/send_message', methods=['POST'])
|
104 |
def send_message():
|
@@ -110,12 +107,11 @@ def send_message():
|
|
110 |
if not message:
|
111 |
return jsonify({'error': 'No message provided'}), 400
|
112 |
|
113 |
-
|
114 |
-
if
|
115 |
-
|
116 |
|
117 |
# Perform web search if enabled
|
118 |
-
web_results = None
|
119 |
if web_search_enabled:
|
120 |
web_results = perform_web_search(message)
|
121 |
if web_results:
|
@@ -123,21 +119,23 @@ def send_message():
|
|
123 |
message = f"""Question: {message}\n\nRésultats de recherche web:\n{formatted_results}\n\nPourrais-tu analyser ces informations et me donner une réponse complète?"""
|
124 |
|
125 |
# Send message to Gemini
|
126 |
-
response =
|
|
|
|
|
|
|
|
|
127 |
|
128 |
-
|
129 |
-
session['chat_history'].append({
|
130 |
'role': 'user',
|
131 |
'content': message
|
132 |
})
|
133 |
-
session['
|
134 |
'role': 'assistant',
|
135 |
'content': response.text
|
136 |
})
|
137 |
|
138 |
return jsonify({
|
139 |
-
'response': response.text
|
140 |
-
'web_results': web_results
|
141 |
})
|
142 |
|
143 |
except Exception as e:
|
@@ -167,9 +165,11 @@ def upload_file():
|
|
167 |
|
168 |
@app.route('/clear_chat', methods=['POST'])
|
169 |
def clear_chat():
|
170 |
-
session.
|
171 |
-
|
|
|
|
|
172 |
return jsonify({'success': True})
|
173 |
|
174 |
if __name__ == '__main__':
|
175 |
-
app.run(
|
|
|
8 |
import tempfile
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
+
app.secret_key = os.urandom(24)
|
12 |
load_dotenv()
|
13 |
|
14 |
# Configure Google AI
|
|
|
34 |
safety_settings=safety_settings,
|
35 |
system_instruction=SYSTEM_PROMPT)
|
36 |
|
37 |
+
# Store chat sessions in a dictionary
|
38 |
+
chat_sessions = {}
|
39 |
+
|
40 |
def perform_web_search(query):
|
41 |
conn = http.client.HTTPSConnection("google.serper.dev")
|
42 |
payload = json.dumps({"q": query})
|
|
|
79 |
'link': item['link']
|
80 |
})
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
return result
|
83 |
|
84 |
UPLOAD_FOLDER = 'temp'
|
|
|
91 |
|
92 |
@app.route('/')
|
93 |
def home():
|
94 |
+
if 'user_id' not in session:
|
95 |
+
session['user_id'] = os.urandom(16).hex()
|
96 |
+
if 'messages' not in session:
|
97 |
+
session['messages'] = []
|
98 |
+
return render_template('index.html', messages=session['messages'])
|
99 |
|
100 |
@app.route('/send_message', methods=['POST'])
|
101 |
def send_message():
|
|
|
107 |
if not message:
|
108 |
return jsonify({'error': 'No message provided'}), 400
|
109 |
|
110 |
+
user_id = session.get('user_id')
|
111 |
+
if user_id not in chat_sessions:
|
112 |
+
chat_sessions[user_id] = model.start_chat(history=[])
|
113 |
|
114 |
# Perform web search if enabled
|
|
|
115 |
if web_search_enabled:
|
116 |
web_results = perform_web_search(message)
|
117 |
if web_results:
|
|
|
119 |
message = f"""Question: {message}\n\nRésultats de recherche web:\n{formatted_results}\n\nPourrais-tu analyser ces informations et me donner une réponse complète?"""
|
120 |
|
121 |
# Send message to Gemini
|
122 |
+
response = chat_sessions[user_id].send_message(message)
|
123 |
+
|
124 |
+
# Update message history in session
|
125 |
+
if 'messages' not in session:
|
126 |
+
session['messages'] = []
|
127 |
|
128 |
+
session['messages'].append({
|
|
|
129 |
'role': 'user',
|
130 |
'content': message
|
131 |
})
|
132 |
+
session['messages'].append({
|
133 |
'role': 'assistant',
|
134 |
'content': response.text
|
135 |
})
|
136 |
|
137 |
return jsonify({
|
138 |
+
'response': response.text
|
|
|
139 |
})
|
140 |
|
141 |
except Exception as e:
|
|
|
165 |
|
166 |
@app.route('/clear_chat', methods=['POST'])
|
167 |
def clear_chat():
|
168 |
+
user_id = session.get('user_id')
|
169 |
+
if user_id in chat_sessions:
|
170 |
+
del chat_sessions[user_id]
|
171 |
+
session['messages'] = []
|
172 |
return jsonify({'success': True})
|
173 |
|
174 |
if __name__ == '__main__':
|
175 |
+
app.run()
|