File size: 2,198 Bytes
e1a0db6
a206d46
e1a0db6
a206d46
 
 
 
a1c3950
a206d46
 
 
 
 
 
 
 
 
 
a1c3950
 
 
 
a206d46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1c3950
 
 
a206d46
 
a1c3950
 
a206d46
e1a0db6
a206d46
d8ddaee
a1c3950
a206d46
 
e1a0db6
a206d46
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import streamlit as st
import openai
import pandas as pd
import subprocess
import os
import io
import contextlib
import re

# Setup OpenAI
openai.api_key = os.getenv("OPENAI_API_KEY")

st.set_page_config(page_title="🧠 Build Any Model", layout="centered")
st.title("πŸ€– Dynamic Code Generator Chatbot")

prompt = st.text_input("🧠 Tell me what you want to build:")
uploaded_file = st.file_uploader("πŸ“‚ Upload your dataset (CSV)", type=["csv"])

def replace_dataset_paths(code: str) -> str:
    """Replace all pd.read_csv(...) with in-memory file reference 'uploaded_file'."""
    return re.sub(r"pd\.read_csv\(['\"](.*?)['\"]\)", "pd.read_csv(uploaded_file)", code)

if st.button("πŸš€ Generate and Run"):
    if prompt and uploaded_file:
        df = pd.read_csv(uploaded_file)
        st.success("βœ… File loaded successfully.")
        
        # Call OpenAI to generate code
        system_prompt = "You are a Python code generator. Only output clean, executable Python code inside one code block. No explanation."
        client = openai.OpenAI()
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": prompt}
            ],
            temperature=0.3,
        )
        code = response.choices[0].message.content
        if "```python" in code:
            code = code.split("```python")[-1].split("```")[0].strip()

        # πŸ” Replace any pd.read_csv(...) with the uploaded_file reference
        code = replace_dataset_paths(code)
        
        st.code(code, language="python")

        # πŸ”„ Inject uploaded_file as a runtime variable
        output_buffer = io.StringIO()
        try:
            with contextlib.redirect_stdout(output_buffer):
                uploaded_file.seek(0)
                exec(code, {"uploaded_file": uploaded_file})
            st.success("βœ… Code executed successfully.")
            st.text(output_buffer.getvalue())
        except Exception as e:
            st.error(f"❌ Error during execution: {e}")
    else:
        st.warning("⚠️ Please upload a dataset and enter a prompt.")