Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import zipfile
|
|
3 |
import uuid
|
4 |
import google.generativeai as genai
|
5 |
from flask import Flask, request, jsonify, render_template, session, send_from_directory
|
6 |
-
from flask_session import Session
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
# --- API and App Configuration ---
|
@@ -14,10 +14,9 @@ app.config["SECRET_KEY"] = os.environ.get("FLASK_SECRET_KEY", 'default-insecure-
|
|
14 |
|
15 |
# --- SERVER-SIDE SESSION CONFIGURATION ---
|
16 |
app.config["SESSION_PERMANENT"] = False
|
17 |
-
app.config["SESSION_TYPE"] = "filesystem"
|
18 |
-
app.config['SESSION_FILE_DIR'] = './flask_session'
|
19 |
-
Session(app)
|
20 |
-
# --- END OF SESSION CONFIGURATION ---
|
21 |
|
22 |
# Configure the Gemini API client
|
23 |
try:
|
@@ -40,8 +39,6 @@ if not os.path.exists(UPLOAD_FOLDER):
|
|
40 |
if not os.path.exists('./flask_session'):
|
41 |
os.makedirs('./flask_session')
|
42 |
|
43 |
-
|
44 |
-
# --- Helper Functions (No Changes) ---
|
45 |
def get_project_files(project_path):
|
46 |
file_data = {}
|
47 |
for root, _, files in os.walk(project_path):
|
@@ -57,8 +54,6 @@ def get_project_files(project_path):
|
|
57 |
file_data[relative_path] = f"Error reading file: {e}"
|
58 |
return file_data
|
59 |
|
60 |
-
|
61 |
-
# --- Flask Routes ---
|
62 |
@app.route('/')
|
63 |
def index():
|
64 |
if 'project_id' not in session:
|
@@ -66,7 +61,6 @@ def index():
|
|
66 |
session['chat_history'] = []
|
67 |
return render_template('index.html')
|
68 |
|
69 |
-
|
70 |
@app.route('/upload', methods=['POST'])
|
71 |
def upload_project():
|
72 |
if 'project_id' not in session:
|
@@ -75,6 +69,7 @@ def upload_project():
|
|
75 |
|
76 |
if 'project_zip' not in request.files:
|
77 |
return jsonify({"error": "No file part"}), 400
|
|
|
78 |
file = request.files['project_zip']
|
79 |
if file.filename == '':
|
80 |
return jsonify({"error": "No selected file"}), 400
|
@@ -82,16 +77,25 @@ def upload_project():
|
|
82 |
if file and file.filename.endswith('.zip'):
|
83 |
project_id = session['project_id']
|
84 |
project_path = os.path.join(app.config['UPLOAD_FOLDER'], project_id)
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
zip_path = os.path.join(project_path, file.filename)
|
88 |
file.save(zip_path)
|
|
|
89 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
90 |
zip_ref.extractall(project_path)
|
91 |
os.remove(zip_path)
|
92 |
|
93 |
project_files = get_project_files(project_path)
|
94 |
-
|
95 |
initial_context = "The user has uploaded a new project. Here is the file structure and content:\n\n"
|
96 |
for path, content in project_files.items():
|
97 |
initial_context += f"**File:** `{path}`\n```\n{content}\n```\n\n"
|
@@ -103,7 +107,7 @@ def upload_project():
|
|
103 |
session.modified = True
|
104 |
|
105 |
frontend_chat_history = [
|
106 |
-
{"role": "user", "content":
|
107 |
{"role": "assistant", "content": "I have analyzed the project. I am ready to help. How can I assist you with your code?"}
|
108 |
]
|
109 |
|
@@ -131,15 +135,15 @@ def chat():
|
|
131 |
session['chat_history'].append({"role": "user", "parts": [user_message]})
|
132 |
session.modified = True
|
133 |
|
134 |
-
chat_session = model.start_chat(history=session['chat_history'])
|
135 |
-
|
136 |
try:
|
|
|
137 |
response = chat_session.send_message(user_message)
|
138 |
ai_response = response.text
|
139 |
|
140 |
session['chat_history'].append({"role": "model", "parts": [ai_response]})
|
141 |
session.modified = True
|
142 |
|
|
|
143 |
if "```" in ai_response:
|
144 |
lines = ai_response.split('\n')
|
145 |
file_path_line = lines[0].strip()
|
@@ -168,51 +172,68 @@ def chat():
|
|
168 |
except Exception as e:
|
169 |
return jsonify({"error": str(e)}), 500
|
170 |
|
171 |
-
|
172 |
@app.route('/file_tree')
|
173 |
def get_file_tree():
|
174 |
project_id = session.get('project_id')
|
175 |
if not project_id:
|
176 |
-
return jsonify({"error": "No project in session."}), 400
|
|
|
177 |
project_path = os.path.join(app.config['UPLOAD_FOLDER'], project_id)
|
178 |
if not os.path.exists(project_path):
|
179 |
return jsonify({"file_tree": []})
|
180 |
-
|
181 |
-
|
|
|
|
|
|
|
|
|
182 |
|
183 |
@app.route('/file_content')
|
184 |
def get_file_content():
|
185 |
project_id = session.get('project_id')
|
186 |
file_path = request.args.get('path')
|
187 |
-
|
188 |
-
|
|
|
|
|
|
|
|
|
189 |
full_path = os.path.join(app.config['UPLOAD_FOLDER'], project_id, file_path)
|
190 |
if not os.path.exists(full_path):
|
191 |
return jsonify({"error": "File not found"}), 404
|
|
|
192 |
try:
|
193 |
with open(full_path, 'r', encoding='utf-8', errors='ignore') as f:
|
194 |
content = f.read()
|
195 |
-
return jsonify({"
|
196 |
except Exception as e:
|
197 |
return jsonify({"error": str(e)}), 500
|
198 |
|
199 |
-
|
200 |
@app.route('/download')
|
201 |
def download_project():
|
202 |
project_id = session.get('project_id')
|
203 |
if not project_id:
|
204 |
-
return "No project to download.", 404
|
|
|
205 |
project_path = os.path.join(app.config['UPLOAD_FOLDER'], project_id)
|
|
|
|
|
|
|
206 |
zip_filename = f"project_{project_id}.zip"
|
207 |
zip_path = os.path.join(app.config['UPLOAD_FOLDER'], zip_filename)
|
|
|
|
|
|
|
|
|
|
|
208 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
209 |
for root, _, files in os.walk(project_path):
|
210 |
for file in files:
|
211 |
file_path = os.path.join(root, file)
|
212 |
arcname = os.path.relpath(file_path, project_path)
|
213 |
zipf.write(file_path, arcname)
|
|
|
214 |
return send_from_directory(app.config['UPLOAD_FOLDER'], zip_filename, as_attachment=True)
|
215 |
|
216 |
-
|
217 |
if __name__ == '__main__':
|
218 |
app.run(host="0.0.0.0", port=7860)
|
|
|
3 |
import uuid
|
4 |
import google.generativeai as genai
|
5 |
from flask import Flask, request, jsonify, render_template, session, send_from_directory
|
6 |
+
from flask_session import Session
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
# --- API and App Configuration ---
|
|
|
14 |
|
15 |
# --- SERVER-SIDE SESSION CONFIGURATION ---
|
16 |
app.config["SESSION_PERMANENT"] = False
|
17 |
+
app.config["SESSION_TYPE"] = "filesystem"
|
18 |
+
app.config['SESSION_FILE_DIR'] = './flask_session'
|
19 |
+
Session(app)
|
|
|
20 |
|
21 |
# Configure the Gemini API client
|
22 |
try:
|
|
|
39 |
if not os.path.exists('./flask_session'):
|
40 |
os.makedirs('./flask_session')
|
41 |
|
|
|
|
|
42 |
def get_project_files(project_path):
|
43 |
file_data = {}
|
44 |
for root, _, files in os.walk(project_path):
|
|
|
54 |
file_data[relative_path] = f"Error reading file: {e}"
|
55 |
return file_data
|
56 |
|
|
|
|
|
57 |
@app.route('/')
|
58 |
def index():
|
59 |
if 'project_id' not in session:
|
|
|
61 |
session['chat_history'] = []
|
62 |
return render_template('index.html')
|
63 |
|
|
|
64 |
@app.route('/upload', methods=['POST'])
|
65 |
def upload_project():
|
66 |
if 'project_id' not in session:
|
|
|
69 |
|
70 |
if 'project_zip' not in request.files:
|
71 |
return jsonify({"error": "No file part"}), 400
|
72 |
+
|
73 |
file = request.files['project_zip']
|
74 |
if file.filename == '':
|
75 |
return jsonify({"error": "No selected file"}), 400
|
|
|
77 |
if file and file.filename.endswith('.zip'):
|
78 |
project_id = session['project_id']
|
79 |
project_path = os.path.join(app.config['UPLOAD_FOLDER'], project_id)
|
80 |
+
|
81 |
+
# Clear existing project files if they exist
|
82 |
+
if os.path.exists(project_path):
|
83 |
+
for root, dirs, files in os.walk(project_path, topdown=False):
|
84 |
+
for name in files:
|
85 |
+
os.remove(os.path.join(root, name))
|
86 |
+
for name in dirs:
|
87 |
+
os.rmdir(os.path.join(root, name))
|
88 |
+
|
89 |
+
os.makedirs(project_path, exist_ok=True)
|
90 |
zip_path = os.path.join(project_path, file.filename)
|
91 |
file.save(zip_path)
|
92 |
+
|
93 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
94 |
zip_ref.extractall(project_path)
|
95 |
os.remove(zip_path)
|
96 |
|
97 |
project_files = get_project_files(project_path)
|
98 |
+
|
99 |
initial_context = "The user has uploaded a new project. Here is the file structure and content:\n\n"
|
100 |
for path, content in project_files.items():
|
101 |
initial_context += f"**File:** `{path}`\n```\n{content}\n```\n\n"
|
|
|
107 |
session.modified = True
|
108 |
|
109 |
frontend_chat_history = [
|
110 |
+
{"role": "user", "content": "Project context sent to AI."},
|
111 |
{"role": "assistant", "content": "I have analyzed the project. I am ready to help. How can I assist you with your code?"}
|
112 |
]
|
113 |
|
|
|
135 |
session['chat_history'].append({"role": "user", "parts": [user_message]})
|
136 |
session.modified = True
|
137 |
|
|
|
|
|
138 |
try:
|
139 |
+
chat_session = model.start_chat(history=session['chat_history'])
|
140 |
response = chat_session.send_message(user_message)
|
141 |
ai_response = response.text
|
142 |
|
143 |
session['chat_history'].append({"role": "model", "parts": [ai_response]})
|
144 |
session.modified = True
|
145 |
|
146 |
+
# Handle file modifications from AI response
|
147 |
if "```" in ai_response:
|
148 |
lines = ai_response.split('\n')
|
149 |
file_path_line = lines[0].strip()
|
|
|
172 |
except Exception as e:
|
173 |
return jsonify({"error": str(e)}), 500
|
174 |
|
|
|
175 |
@app.route('/file_tree')
|
176 |
def get_file_tree():
|
177 |
project_id = session.get('project_id')
|
178 |
if not project_id:
|
179 |
+
return jsonify({"error": "No project in session. Please upload a project first."}), 400
|
180 |
+
|
181 |
project_path = os.path.join(app.config['UPLOAD_FOLDER'], project_id)
|
182 |
if not os.path.exists(project_path):
|
183 |
return jsonify({"file_tree": []})
|
184 |
+
|
185 |
+
try:
|
186 |
+
project_files = get_project_files(project_path)
|
187 |
+
return jsonify({"file_tree": list(project_files.keys())})
|
188 |
+
except Exception as e:
|
189 |
+
return jsonify({"error": str(e)}), 500
|
190 |
|
191 |
@app.route('/file_content')
|
192 |
def get_file_content():
|
193 |
project_id = session.get('project_id')
|
194 |
file_path = request.args.get('path')
|
195 |
+
|
196 |
+
if not project_id:
|
197 |
+
return jsonify({"error": "No project in session. Please upload a project first."}), 400
|
198 |
+
if not file_path:
|
199 |
+
return jsonify({"error": "Missing file path"}), 400
|
200 |
+
|
201 |
full_path = os.path.join(app.config['UPLOAD_FOLDER'], project_id, file_path)
|
202 |
if not os.path.exists(full_path):
|
203 |
return jsonify({"error": "File not found"}), 404
|
204 |
+
|
205 |
try:
|
206 |
with open(full_path, 'r', encoding='utf-8', errors='ignore') as f:
|
207 |
content = f.read()
|
208 |
+
return jsonify({"content": content})
|
209 |
except Exception as e:
|
210 |
return jsonify({"error": str(e)}), 500
|
211 |
|
|
|
212 |
@app.route('/download')
|
213 |
def download_project():
|
214 |
project_id = session.get('project_id')
|
215 |
if not project_id:
|
216 |
+
return "No project to download. Please upload a project first.", 404
|
217 |
+
|
218 |
project_path = os.path.join(app.config['UPLOAD_FOLDER'], project_id)
|
219 |
+
if not os.path.exists(project_path):
|
220 |
+
return "Project files not found.", 404
|
221 |
+
|
222 |
zip_filename = f"project_{project_id}.zip"
|
223 |
zip_path = os.path.join(app.config['UPLOAD_FOLDER'], zip_filename)
|
224 |
+
|
225 |
+
# Remove existing zip if it exists
|
226 |
+
if os.path.exists(zip_path):
|
227 |
+
os.remove(zip_path)
|
228 |
+
|
229 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
230 |
for root, _, files in os.walk(project_path):
|
231 |
for file in files:
|
232 |
file_path = os.path.join(root, file)
|
233 |
arcname = os.path.relpath(file_path, project_path)
|
234 |
zipf.write(file_path, arcname)
|
235 |
+
|
236 |
return send_from_directory(app.config['UPLOAD_FOLDER'], zip_filename, as_attachment=True)
|
237 |
|
|
|
238 |
if __name__ == '__main__':
|
239 |
app.run(host="0.0.0.0", port=7860)
|