Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,16 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, jsonify
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
import http.client
|
| 6 |
import json
|
| 7 |
from werkzeug.utils import secure_filename
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
app.config['UPLOAD_FOLDER'] = 'temp'
|
| 11 |
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
|
|
|
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
|
|
@@ -22,10 +24,11 @@ safety_settings = [
|
|
| 22 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 23 |
]
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
def perform_web_search(query):
|
| 31 |
conn = http.client.HTTPSConnection("google.serper.dev")
|
|
@@ -67,7 +70,9 @@ def format_search_results(data):
|
|
| 67 |
|
| 68 |
@app.route('/')
|
| 69 |
def home():
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
@app.route('/chat', methods=['POST'])
|
| 73 |
def chat():
|
|
@@ -75,6 +80,9 @@ def chat():
|
|
| 75 |
prompt = data.get('message')
|
| 76 |
web_search_enabled = data.get('web_search', False)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
try:
|
| 79 |
web_results = None
|
| 80 |
if web_search_enabled:
|
|
@@ -83,8 +91,31 @@ def chat():
|
|
| 83 |
formatted_results = format_search_results(web_results)
|
| 84 |
prompt = f"""Question: {prompt}\n\nRésultats de recherche web:\n{formatted_results}\n\nPourrais-tu analyser ces informations et me donner une réponse complète?"""
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
return jsonify({"error": str(e)}), 500
|
|
@@ -109,6 +140,11 @@ def upload_file():
|
|
| 109 |
except Exception as e:
|
| 110 |
return jsonify({"error": str(e)}), 500
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
if __name__ == '__main__':
|
| 113 |
os.makedirs("temp", exist_ok=True)
|
| 114 |
app.run(debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify, session
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
import http.client
|
| 6 |
import json
|
| 7 |
from werkzeug.utils import secure_filename
|
| 8 |
+
import markdown2
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
app.config['UPLOAD_FOLDER'] = 'temp'
|
| 12 |
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
|
| 13 |
+
app.secret_key = 'your-secret-key-here' # Change this to a secure secret key
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
|
|
|
|
| 24 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 25 |
]
|
| 26 |
|
| 27 |
+
def get_chat_model():
|
| 28 |
+
return genai.GenerativeModel('gemini-2.0-flash-exp',
|
| 29 |
+
tools='code_execution',
|
| 30 |
+
safety_settings=safety_settings,
|
| 31 |
+
system_instruction="Tu es un assistant intelligent. ton but est d'assister au mieux que tu peux. tu as été créé par Aenir et tu t'appelles Mariam")
|
| 32 |
|
| 33 |
def perform_web_search(query):
|
| 34 |
conn = http.client.HTTPSConnection("google.serper.dev")
|
|
|
|
| 70 |
|
| 71 |
@app.route('/')
|
| 72 |
def home():
|
| 73 |
+
if 'chat_history' not in session:
|
| 74 |
+
session['chat_history'] = []
|
| 75 |
+
return render_template('index.html', chat_history=session['chat_history'])
|
| 76 |
|
| 77 |
@app.route('/chat', methods=['POST'])
|
| 78 |
def chat():
|
|
|
|
| 80 |
prompt = data.get('message')
|
| 81 |
web_search_enabled = data.get('web_search', False)
|
| 82 |
|
| 83 |
+
if 'chat' not in session:
|
| 84 |
+
session['chat'] = get_chat_model().start_chat(history=[])
|
| 85 |
+
|
| 86 |
try:
|
| 87 |
web_results = None
|
| 88 |
if web_search_enabled:
|
|
|
|
| 91 |
formatted_results = format_search_results(web_results)
|
| 92 |
prompt = f"""Question: {prompt}\n\nRésultats de recherche web:\n{formatted_results}\n\nPourrais-tu analyser ces informations et me donner une réponse complète?"""
|
| 93 |
|
| 94 |
+
chat = session['chat']
|
| 95 |
+
response = chat.send_message(prompt)
|
| 96 |
+
|
| 97 |
+
# Convert Markdown to HTML for the response
|
| 98 |
+
response_html = markdown2.markdown(response.text, extras=["fenced-code-blocks", "tables"])
|
| 99 |
+
|
| 100 |
+
# Update chat history
|
| 101 |
+
if 'chat_history' not in session:
|
| 102 |
+
session['chat_history'] = []
|
| 103 |
+
|
| 104 |
+
session['chat_history'].append({
|
| 105 |
+
'role': 'user',
|
| 106 |
+
'content': prompt
|
| 107 |
+
})
|
| 108 |
+
session['chat_history'].append({
|
| 109 |
+
'role': 'assistant',
|
| 110 |
+
'content': response.text,
|
| 111 |
+
'content_html': response_html
|
| 112 |
+
})
|
| 113 |
+
session.modified = True
|
| 114 |
+
|
| 115 |
+
return jsonify({
|
| 116 |
+
"response": response.text,
|
| 117 |
+
"response_html": response_html
|
| 118 |
+
})
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 140 |
except Exception as e:
|
| 141 |
return jsonify({"error": str(e)}), 500
|
| 142 |
|
| 143 |
+
@app.route('/clear', methods=['POST'])
|
| 144 |
+
def clear_history():
|
| 145 |
+
session.clear()
|
| 146 |
+
return jsonify({"success": True})
|
| 147 |
+
|
| 148 |
if __name__ == '__main__':
|
| 149 |
os.makedirs("temp", exist_ok=True)
|
| 150 |
app.run(debug=True)
|