Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,51 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
scaler = StandardScaler()
|
| 32 |
-
X_train = scaler.fit_transform(X_train)
|
| 33 |
-
X_test = scaler.transform(X_test)
|
| 34 |
-
|
| 35 |
-
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 36 |
-
model.fit(X_train, y_train)
|
| 37 |
-
y_pred = model.predict(X_test)
|
| 38 |
-
|
| 39 |
-
accuracy = accuracy_score(y_test, y_pred)
|
| 40 |
-
report = classification_report(y_test, y_pred, output_dict=False)
|
| 41 |
-
|
| 42 |
-
st.write("### β
Accuracy:")
|
| 43 |
-
st.write(f"{accuracy * 100:.2f}%")
|
| 44 |
-
|
| 45 |
-
st.write("### π Classification Report:")
|
| 46 |
-
st.code(report)
|
| 47 |
-
|
| 48 |
except Exception as e:
|
| 49 |
-
st.error(f"β
|
| 50 |
-
else:
|
| 51 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
import pandas as pd
|
| 4 |
+
import subprocess
|
| 5 |
+
import os
|
| 6 |
+
import io
|
| 7 |
+
import contextlib
|
| 8 |
+
|
| 9 |
+
# Setup OpenAI
|
| 10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
|
| 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)
|
| 22 |
+
st.success("β
File loaded successfully.")
|
| 23 |
+
|
| 24 |
+
# Call OpenAI to generate code
|
| 25 |
+
system_prompt = "You are a Python code generator. Only output clean, executable Python code inside one code block. No explanation."
|
| 26 |
+
client = openai.OpenAI()
|
| 27 |
+
response = client.chat.completions.create(
|
| 28 |
+
model="gpt-4o",
|
| 29 |
+
messages=[
|
| 30 |
+
{"role": "system", "content": system_prompt},
|
| 31 |
+
{"role": "user", "content": prompt}
|
| 32 |
+
],
|
| 33 |
+
temperature=0.3,
|
| 34 |
+
)
|
| 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 |
+
# Execute the generated code
|
| 42 |
+
output_buffer = io.StringIO()
|
| 43 |
try:
|
| 44 |
+
with contextlib.redirect_stdout(output_buffer):
|
| 45 |
+
exec(code, {"df": df})
|
| 46 |
+
st.success("β
Code executed successfully.")
|
| 47 |
+
st.text(output_buffer.getvalue())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
+
st.error(f"β Error during execution: {e}")
|
| 50 |
+
else:
|
| 51 |
+
st.warning("β οΈ Please upload a dataset and enter a prompt.")
|