Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,6 @@ import os
|
|
6 |
from flask_cors import CORS
|
7 |
import markdown2
|
8 |
import re
|
9 |
-
import tempfile
|
10 |
-
from werkzeug.utils import secure_filename
|
11 |
|
12 |
# Load environment variables
|
13 |
load_dotenv()
|
@@ -16,29 +14,22 @@ load_dotenv()
|
|
16 |
app = Flask(__name__, static_folder='static')
|
17 |
CORS(app) # Enable CORS for all routes
|
18 |
|
19 |
-
# Configuration
|
20 |
-
UPLOAD_FOLDER = 'uploads'
|
21 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
22 |
-
ALLOWED_EXTENSIONS = {'pdf', 'txt', 'docx', 'pptx', 'xlsx', 'jpg', 'jpeg', 'png'}
|
23 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
24 |
-
|
25 |
# Configure Gemini
|
26 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
27 |
-
model = genai.GenerativeModel('gemini-
|
28 |
-
vision_model = genai.GenerativeModel('gemini-2.5-flash')
|
29 |
-
|
30 |
-
def allowed_file(filename):
|
31 |
-
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
32 |
|
33 |
def convert_markdown_to_html(text):
|
34 |
# Convert markdown to HTML
|
35 |
html = markdown2.markdown(text)
|
36 |
|
37 |
-
# Add custom styling
|
38 |
html = html.replace('<code>', '<code class="code-block">')
|
|
|
|
|
39 |
html = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', html)
|
|
|
|
|
40 |
html = re.sub(r'\*(.*?)\*', r'<em>\1</em>', html)
|
41 |
-
html = html.replace('<a ', '<a target="_blank" rel="noopener noreferrer" ')
|
42 |
|
43 |
return html
|
44 |
|
@@ -47,25 +38,12 @@ def chat():
|
|
47 |
try:
|
48 |
data = request.json
|
49 |
user_message = data.get('message')
|
50 |
-
file_path = data.get('file_path')
|
51 |
|
52 |
-
if not user_message
|
53 |
-
return jsonify({"error": "No message
|
54 |
|
55 |
-
# Generate response
|
56 |
-
|
57 |
-
# Process file with vision model if it's an image
|
58 |
-
if file_path.lower().endswith(('.jpg', '.jpeg', '.png')):
|
59 |
-
response = vision_model.generate_content([user_message, genai.upload_file(file_path)])
|
60 |
-
else:
|
61 |
-
# Process text-based files
|
62 |
-
with open(file_path, 'r') as file:
|
63 |
-
file_content = file.read()
|
64 |
-
prompt = f"{user_message}\n\nFile content:\n{file_content}"
|
65 |
-
response = model.generate_content(prompt)
|
66 |
-
else:
|
67 |
-
# Regular text chat
|
68 |
-
response = model.generate_content(user_message)
|
69 |
|
70 |
# Convert markdown to HTML with styling
|
71 |
formatted_response = convert_markdown_to_html(response.text)
|
@@ -77,29 +55,6 @@ def chat():
|
|
77 |
except Exception as e:
|
78 |
return jsonify({"error": str(e)}), 500
|
79 |
|
80 |
-
@app.route('/upload', methods=['POST'])
|
81 |
-
def upload_file():
|
82 |
-
try:
|
83 |
-
if 'file' not in request.files:
|
84 |
-
return jsonify({"error": "No file part"}), 400
|
85 |
-
|
86 |
-
file = request.files['file']
|
87 |
-
if file.filename == '':
|
88 |
-
return jsonify({"error": "No selected file"}), 400
|
89 |
-
|
90 |
-
if file and allowed_file(file.filename):
|
91 |
-
filename = secure_filename(file.filename)
|
92 |
-
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
93 |
-
file.save(file_path)
|
94 |
-
return jsonify({
|
95 |
-
"message": "File uploaded successfully",
|
96 |
-
"file_path": file_path
|
97 |
-
})
|
98 |
-
else:
|
99 |
-
return jsonify({"error": "File type not allowed"}), 400
|
100 |
-
except Exception as e:
|
101 |
-
return jsonify({"error": str(e)}), 500
|
102 |
-
|
103 |
@app.route('/')
|
104 |
def serve_index():
|
105 |
return send_from_directory('static', 'index.html')
|
@@ -109,4 +64,4 @@ def serve_static(path):
|
|
109 |
return send_from_directory('static', path)
|
110 |
|
111 |
if __name__ == '__main__':
|
112 |
-
app.run(host=
|
|
|
6 |
from flask_cors import CORS
|
7 |
import markdown2
|
8 |
import re
|
|
|
|
|
9 |
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
|
|
14 |
app = Flask(__name__, static_folder='static')
|
15 |
CORS(app) # Enable CORS for all routes
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Configure Gemini
|
18 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
19 |
+
model = genai.GenerativeModel('gemini-pro')
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def convert_markdown_to_html(text):
|
22 |
# Convert markdown to HTML
|
23 |
html = markdown2.markdown(text)
|
24 |
|
25 |
+
# Add custom styling to code blocks
|
26 |
html = html.replace('<code>', '<code class="code-block">')
|
27 |
+
|
28 |
+
# Convert **bold** to <strong> for better visibility
|
29 |
html = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', html)
|
30 |
+
|
31 |
+
# Convert *italic* to <em>
|
32 |
html = re.sub(r'\*(.*?)\*', r'<em>\1</em>', html)
|
|
|
33 |
|
34 |
return html
|
35 |
|
|
|
38 |
try:
|
39 |
data = request.json
|
40 |
user_message = data.get('message')
|
|
|
41 |
|
42 |
+
if not user_message:
|
43 |
+
return jsonify({"error": "No message provided"}), 400
|
44 |
|
45 |
+
# Generate response using Gemini
|
46 |
+
response = model.generate_content(user_message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
# Convert markdown to HTML with styling
|
49 |
formatted_response = convert_markdown_to_html(response.text)
|
|
|
55 |
except Exception as e:
|
56 |
return jsonify({"error": str(e)}), 500
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
@app.route('/')
|
59 |
def serve_index():
|
60 |
return send_from_directory('static', 'index.html')
|
|
|
64 |
return send_from_directory('static', path)
|
65 |
|
66 |
if __name__ == '__main__':
|
67 |
+
app.run(host='0.0.0.0', port=5000)
|