Update app.py
Browse files
app.py
CHANGED
|
@@ -83,6 +83,35 @@ def save_and_play_audio(audio_recorder):
|
|
| 83 |
return None
|
| 84 |
|
| 85 |
def create_file(filename, prompt, response, should_save=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
if not should_save:
|
| 87 |
return
|
| 88 |
if filename.endswith(".txt"):
|
|
|
|
| 83 |
return None
|
| 84 |
|
| 85 |
def create_file(filename, prompt, response, should_save=True):
|
| 86 |
+
if not should_save:
|
| 87 |
+
return
|
| 88 |
+
|
| 89 |
+
# Step 2: Extract base filename without extension
|
| 90 |
+
base_filename, ext = os.path.splitext(filename)
|
| 91 |
+
|
| 92 |
+
# Step 3: Check if the response contains Python code
|
| 93 |
+
has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
|
| 94 |
+
|
| 95 |
+
# Step 4: Write files based on type
|
| 96 |
+
if ext in ['.txt', '.htm', '.md']:
|
| 97 |
+
# Create Prompt file
|
| 98 |
+
with open(f"{base_filename}-Prompt.txt", 'w') as file:
|
| 99 |
+
file.write(prompt)
|
| 100 |
+
|
| 101 |
+
# Create Response file
|
| 102 |
+
with open(f"{base_filename}-Response.md", 'w') as file:
|
| 103 |
+
file.write(response)
|
| 104 |
+
|
| 105 |
+
# Create Code file if Python code is present
|
| 106 |
+
if has_python_code:
|
| 107 |
+
# Extract Python code from the response
|
| 108 |
+
python_code = re.findall(r"```python([\s\S]*?)```", response)[0].strip()
|
| 109 |
+
|
| 110 |
+
with open(f"{base_filename}-Code.py", 'w') as file:
|
| 111 |
+
file.write(python_code)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def create_file_old(filename, prompt, response, should_save=True):
|
| 115 |
if not should_save:
|
| 116 |
return
|
| 117 |
if filename.endswith(".txt"):
|