Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -47,6 +47,63 @@ with col1:
|
|
47 |
choice = st.sidebar.selectbox("Output File Type:", menu)
|
48 |
model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# Read it aloud
|
52 |
def readitaloud(result):
|
@@ -322,62 +379,6 @@ def save_and_play_audio(audio_recorder):
|
|
322 |
return None
|
323 |
|
324 |
|
325 |
-
# Define a context dictionary to maintain the state between exec calls
|
326 |
-
context = {}
|
327 |
-
|
328 |
-
def create_file(filename, prompt, response, should_save=True):
|
329 |
-
if not should_save:
|
330 |
-
return
|
331 |
-
|
332 |
-
# Extract base filename without extension
|
333 |
-
base_filename, ext = os.path.splitext(filename)
|
334 |
-
|
335 |
-
# Initialize the combined content
|
336 |
-
combined_content = ""
|
337 |
-
|
338 |
-
# Add Prompt with markdown title and emoji
|
339 |
-
combined_content += "# Prompt π\n" + prompt + "\n\n"
|
340 |
-
|
341 |
-
# Add Response with markdown title and emoji
|
342 |
-
combined_content += "# Response π¬\n" + response + "\n\n"
|
343 |
-
|
344 |
-
# Check for code blocks in the response
|
345 |
-
resources = re.findall(r"```([\s\S]*?)```", response)
|
346 |
-
for resource in resources:
|
347 |
-
# Check if the resource contains Python code
|
348 |
-
if "python" in resource.lower():
|
349 |
-
# Remove the 'python' keyword from the code block
|
350 |
-
cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
|
351 |
-
|
352 |
-
# Add Code Results title with markdown and emoji
|
353 |
-
combined_content += "# Code Results π\n"
|
354 |
-
|
355 |
-
# Redirect standard output to capture it
|
356 |
-
original_stdout = sys.stdout
|
357 |
-
sys.stdout = io.StringIO()
|
358 |
-
|
359 |
-
# Execute the cleaned Python code within the context
|
360 |
-
try:
|
361 |
-
exec(cleaned_code, context)
|
362 |
-
code_output = sys.stdout.getvalue()
|
363 |
-
combined_content += f"```\n{code_output}\n```\n\n"
|
364 |
-
realtimeEvalResponse = "# Code Results π\n" + "```" + code_output + "```\n\n"
|
365 |
-
st.write(realtimeEvalResponse)
|
366 |
-
|
367 |
-
except Exception as e:
|
368 |
-
combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
|
369 |
-
|
370 |
-
# Restore the original standard output
|
371 |
-
sys.stdout = original_stdout
|
372 |
-
else:
|
373 |
-
# Add non-Python resources with markdown and emoji
|
374 |
-
combined_content += "# Resource π οΈ\n" + "```" + resource + "```\n\n"
|
375 |
-
|
376 |
-
# Save the combined content to a Markdown file
|
377 |
-
if should_save:
|
378 |
-
with open(f"{base_filename}.md", 'w') as file:
|
379 |
-
file.write(combined_content)
|
380 |
-
|
381 |
|
382 |
def truncate_document(document, length):
|
383 |
return document[:length]
|
|
|
47 |
choice = st.sidebar.selectbox("Output File Type:", menu)
|
48 |
model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
|
49 |
|
50 |
+
|
51 |
+
# Define a context dictionary to maintain the state between exec calls
|
52 |
+
context = {}
|
53 |
+
|
54 |
+
def create_file(filename, prompt, response, should_save=True):
|
55 |
+
if not should_save:
|
56 |
+
return
|
57 |
+
|
58 |
+
# Extract base filename without extension
|
59 |
+
base_filename, ext = os.path.splitext(filename)
|
60 |
+
|
61 |
+
# Initialize the combined content
|
62 |
+
combined_content = ""
|
63 |
+
|
64 |
+
# Add Prompt with markdown title and emoji
|
65 |
+
combined_content += "# Prompt π\n" + prompt + "\n\n"
|
66 |
+
|
67 |
+
# Add Response with markdown title and emoji
|
68 |
+
combined_content += "# Response π¬\n" + response + "\n\n"
|
69 |
+
|
70 |
+
# Check for code blocks in the response
|
71 |
+
resources = re.findall(r"```([\s\S]*?)```", response)
|
72 |
+
for resource in resources:
|
73 |
+
# Check if the resource contains Python code
|
74 |
+
if "python" in resource.lower():
|
75 |
+
# Remove the 'python' keyword from the code block
|
76 |
+
cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
|
77 |
+
|
78 |
+
# Add Code Results title with markdown and emoji
|
79 |
+
combined_content += "# Code Results π\n"
|
80 |
+
|
81 |
+
# Redirect standard output to capture it
|
82 |
+
original_stdout = sys.stdout
|
83 |
+
sys.stdout = io.StringIO()
|
84 |
+
|
85 |
+
# Execute the cleaned Python code within the context
|
86 |
+
try:
|
87 |
+
exec(cleaned_code, context)
|
88 |
+
code_output = sys.stdout.getvalue()
|
89 |
+
combined_content += f"```\n{code_output}\n```\n\n"
|
90 |
+
realtimeEvalResponse = "# Code Results π\n" + "```" + code_output + "```\n\n"
|
91 |
+
st.write(realtimeEvalResponse)
|
92 |
+
|
93 |
+
except Exception as e:
|
94 |
+
combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
|
95 |
+
|
96 |
+
# Restore the original standard output
|
97 |
+
sys.stdout = original_stdout
|
98 |
+
else:
|
99 |
+
# Add non-Python resources with markdown and emoji
|
100 |
+
combined_content += "# Resource π οΈ\n" + "```" + resource + "```\n\n"
|
101 |
+
|
102 |
+
# Save the combined content to a Markdown file
|
103 |
+
if should_save:
|
104 |
+
with open(f"{base_filename}.md", 'w') as file:
|
105 |
+
file.write(combined_content)
|
106 |
+
|
107 |
|
108 |
# Read it aloud
|
109 |
def readitaloud(result):
|
|
|
379 |
return None
|
380 |
|
381 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
382 |
|
383 |
def truncate_document(document, length):
|
384 |
return document[:length]
|