Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import subprocess
|
|
| 5 |
import os
|
| 6 |
import io
|
| 7 |
import contextlib
|
|
|
|
| 8 |
|
| 9 |
# Setup OpenAI
|
| 10 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
@@ -13,9 +14,12 @@ st.set_page_config(page_title="π§ Build Any Model", layout="centered")
|
|
| 13 |
st.title("π€ Dynamic Code Generator Chatbot")
|
| 14 |
|
| 15 |
prompt = st.text_input("π§ Tell me what you want to build:")
|
| 16 |
-
|
| 17 |
uploaded_file = st.file_uploader("π Upload your dataset (CSV)", type=["csv"])
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
if st.button("π Generate and Run"):
|
| 20 |
if prompt and uploaded_file:
|
| 21 |
df = pd.read_csv(uploaded_file)
|
|
@@ -35,14 +39,17 @@ if st.button("π Generate and Run"):
|
|
| 35 |
code = response.choices[0].message.content
|
| 36 |
if "```python" in code:
|
| 37 |
code = code.split("```python")[-1].split("```")[0].strip()
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
st.code(code, language="python")
|
| 40 |
-
|
| 41 |
-
#
|
| 42 |
output_buffer = io.StringIO()
|
| 43 |
try:
|
| 44 |
with contextlib.redirect_stdout(output_buffer):
|
| 45 |
-
exec(code, {"
|
| 46 |
st.success("β
Code executed successfully.")
|
| 47 |
st.text(output_buffer.getvalue())
|
| 48 |
except Exception as e:
|
|
|
|
| 5 |
import os
|
| 6 |
import io
|
| 7 |
import contextlib
|
| 8 |
+
import re
|
| 9 |
|
| 10 |
# Setup OpenAI
|
| 11 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 14 |
st.title("π€ Dynamic Code Generator Chatbot")
|
| 15 |
|
| 16 |
prompt = st.text_input("π§ Tell me what you want to build:")
|
|
|
|
| 17 |
uploaded_file = st.file_uploader("π Upload your dataset (CSV)", type=["csv"])
|
| 18 |
|
| 19 |
+
def replace_dataset_paths(code: str) -> str:
|
| 20 |
+
"""Replace all pd.read_csv(...) with in-memory file reference 'uploaded_file'."""
|
| 21 |
+
return re.sub(r"pd\.read_csv\(['\"](.*?)['\"]\)", "pd.read_csv(uploaded_file)", code)
|
| 22 |
+
|
| 23 |
if st.button("π Generate and Run"):
|
| 24 |
if prompt and uploaded_file:
|
| 25 |
df = pd.read_csv(uploaded_file)
|
|
|
|
| 39 |
code = response.choices[0].message.content
|
| 40 |
if "```python" in code:
|
| 41 |
code = code.split("```python")[-1].split("```")[0].strip()
|
| 42 |
+
|
| 43 |
+
# π Replace any pd.read_csv(...) with the uploaded_file reference
|
| 44 |
+
code = replace_dataset_paths(code)
|
| 45 |
|
| 46 |
st.code(code, language="python")
|
| 47 |
+
|
| 48 |
+
# π Inject uploaded_file as a runtime variable
|
| 49 |
output_buffer = io.StringIO()
|
| 50 |
try:
|
| 51 |
with contextlib.redirect_stdout(output_buffer):
|
| 52 |
+
exec(code, {"uploaded_file": uploaded_file})
|
| 53 |
st.success("β
Code executed successfully.")
|
| 54 |
st.text(output_buffer.getvalue())
|
| 55 |
except Exception as e:
|