AliInamdar commited on
Commit
a206d46
Β·
verified Β·
1 Parent(s): 6e4de9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -47
app.py CHANGED
@@ -1,51 +1,51 @@
1
  import streamlit as st
 
2
  import pandas as pd
3
- from sklearn.model_selection import train_test_split
4
- from sklearn.ensemble import RandomForestClassifier
5
- from sklearn.metrics import accuracy_score, classification_report
6
- from sklearn.preprocessing import StandardScaler
7
-
8
- st.set_page_config(page_title="Random Forest Diabetes Classifier", layout="centered")
9
- st.title("πŸ‘¨πŸ»β€πŸ’»Dynamic Code Generating ChatBotπŸ€–")
10
-
11
- uploaded_file = st.file_uploader("πŸ“‚ Upload your CSV dataset", type=["csv"])
12
-
13
- if uploaded_file:
14
- df = pd.read_csv(uploaded_file)
15
- st.success("βœ… File loaded successfully!")
16
- st.write("### Preview of Dataset:")
17
- st.dataframe(df.head())
18
-
19
- all_columns = df.columns.tolist()
20
-
21
- target_column = st.selectbox("🎯 Select the target column (diabetes outcome)", all_columns)
22
- feature_columns = st.multiselect("πŸ› οΈ Select feature columns", [col for col in all_columns if col != target_column])
23
-
24
- if st.button("πŸš€ Run Random Forest Classifier"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  try:
26
- X = df[feature_columns]
27
- y = df[target_column]
28
-
29
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
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"❌ An error occurred: {e}")
50
- else:
51
- st.info("πŸ‘ˆ Upload a CSV file to begin.")
 
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.")