ruslanmv commited on
Commit
b956ac5
·
1 Parent(s): 306849a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -51
app.py CHANGED
@@ -93,6 +93,7 @@ def is_project_loaded():
93
  extraction_dir = "extraction"
94
  pkl_files = [f for f in os.listdir(extraction_dir) if f.endswith('.pkl')]
95
  return bool(pkl_files)
 
96
  # --- Gradio UI Components ---
97
 
98
  # Chat Interface
@@ -101,7 +102,7 @@ def chat_ui(query, history, mode):
101
  api_key = load_api_key()
102
  if not api_key:
103
  return "Error: OpenAI API key not set. Please set the API key in the Settings tab.", []
104
-
105
  if not is_project_loaded():
106
  return "Error: No project loaded. Please upload and process a ZIP file first.", []
107
 
@@ -121,59 +122,35 @@ def chat_ui(query, history, mode):
121
  response = "An error occurred during processing. Please check the logs."
122
 
123
  if mode == "developer":
124
- extracted_files = extract_files_from_response(response)
125
-
126
- # Format the output for developer mode
127
- developer_response = ""
128
- for filepath, content in extracted_files.items():
129
- developer_response += f"**{filepath}:**\n`python\n{content}\n`\n\n"
130
-
131
- history.append((query, developer_response))
132
- # Return history and an empty string for the text output (as it's handled by the chatbot)
133
- return history, history
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  else:
136
  # Format the output for non-developer modes
137
- formatted_response = response.replace('\n', ' \n') # Use two spaces for markdown line breaks
138
- history.append((query, formatted_response))
139
- # Return history and an empty string for the text output (as it's handled by the chatbot)
140
- return history, history
141
-
142
-
143
- def extract_files_from_response(response):
144
- """
145
- Parses the LLM response to extract file paths and their corresponding code content.
146
-
147
- Args:
148
- response (str): The raw response string from the LLM.
149
-
150
- Returns:
151
- dict: A dictionary where keys are file paths and values are the code content of each file.
152
- """
153
- files = {}
154
- current_file = None
155
- current_content = []
156
-
157
- for line in response.splitlines():
158
- if line.startswith("--- BEGIN FILE:"):
159
- if current_file is not None:
160
- # Save previous file content
161
- files[current_file] = "\n".join(current_content)
162
-
163
- # Start a new file
164
- current_file = line.replace("--- BEGIN FILE:", "").strip()
165
- current_content = []
166
- elif line.startswith("--- END FILE:"):
167
- if current_file is not None:
168
- # Save current file content
169
- files[current_file] = "\n".join(current_content)
170
- current_file = None
171
- current_content = []
172
- elif current_file is not None:
173
- # Append line to current file content
174
- current_content.append(line)
175
-
176
- return files
177
 
178
  # ZIP Processing Interface
179
  zip_iface = gr.Interface(
 
93
  extraction_dir = "extraction"
94
  pkl_files = [f for f in os.listdir(extraction_dir) if f.endswith('.pkl')]
95
  return bool(pkl_files)
96
+
97
  # --- Gradio UI Components ---
98
 
99
  # Chat Interface
 
102
  api_key = load_api_key()
103
  if not api_key:
104
  return "Error: OpenAI API key not set. Please set the API key in the Settings tab.", []
105
+
106
  if not is_project_loaded():
107
  return "Error: No project loaded. Please upload and process a ZIP file first.", []
108
 
 
122
  response = "An error occurred during processing. Please check the logs."
123
 
124
  if mode == "developer":
125
+ # Split the response into chunks based on "---"
126
+ chunks = response.split("---")
127
+ formatted_response_parts = []
128
+
129
+ for chunk in chunks:
130
+ if chunk.strip().startswith("BEGIN FILE:"):
131
+ # Extract filepath and code content
132
+ filepath = chunk.split("BEGIN FILE:")[1].split("\n")[0].strip()
133
+ code_content = chunk.replace(f"BEGIN FILE: {filepath}\n", "").strip()
134
+
135
+ # Remove "END FILE:" and its associated filepath if present at the end.
136
+ if code_content.rfind("END FILE:") != -1:
137
+ end_file_index = code_content.rfind("END FILE:")
138
+ code_content = code_content[:end_file_index].strip()
139
+
140
+ formatted_response_parts.append(f"**{filepath}:**\n`python\n{code_content}\n`")
141
+ elif chunk.strip():
142
+ formatted_response_parts.append(chunk.strip())
143
+
144
+ # Join the formatted parts with separators
145
+ formatted_response = "\n---\n".join(formatted_response_parts)
146
 
147
  else:
148
  # Format the output for non-developer modes
149
+ formatted_response = response.replace('\n', ' \n')
150
+
151
+ history.append((query, formatted_response))
152
+
153
+ return history, history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
  # ZIP Processing Interface
156
  zip_iface = gr.Interface(