Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,116 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from datetime import datetime
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# ----------
|
| 5 |
st.set_page_config(
|
| 6 |
page_title="Code Assistant",
|
| 7 |
page_icon="🧠",
|
| 8 |
-
layout="
|
| 9 |
)
|
| 10 |
|
| 11 |
-
# ---------- State ----------
|
| 12 |
if "history" not in st.session_state:
|
| 13 |
st.session_state.history = []
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# ---------- Core Logic ----------
|
| 16 |
@st.cache_data(show_spinner=False)
|
| 17 |
-
def generate_code(prompt: str) -> str:
|
| 18 |
"""
|
| 19 |
-
Placeholder for
|
| 20 |
-
Cached to avoid recomputation for identical prompts.
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def example():
|
| 27 |
-
print("
|
| 28 |
"""
|
| 29 |
|
| 30 |
-
def handle_generation(prompt
|
| 31 |
if not prompt.strip():
|
| 32 |
st.warning("Prompt cannot be empty.")
|
| 33 |
return
|
| 34 |
|
| 35 |
with st.spinner("Generating code…"):
|
| 36 |
-
|
| 37 |
|
| 38 |
st.session_state.history.append({
|
| 39 |
"timestamp": datetime.now().strftime("%H:%M:%S"),
|
|
|
|
|
|
|
| 40 |
"prompt": prompt,
|
| 41 |
-
"output":
|
| 42 |
})
|
| 43 |
|
| 44 |
-
# ----------
|
| 45 |
-
st.
|
| 46 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
generate_clicked = st.button("Generate", use_container_width=True)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
clear_clicked = st.button("Clear History", use_container_width=True)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# ---------- Output ----------
|
| 70 |
if st.session_state.history:
|
|
@@ -72,5 +118,13 @@ if st.session_state.history:
|
|
| 72 |
st.subheader("Generation History")
|
| 73 |
|
| 74 |
for item in reversed(st.session_state.history):
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
st.code(item["output"], language="python")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from datetime import datetime
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from io import StringIO
|
| 5 |
|
| 6 |
+
# ---------- Page Config ----------
|
| 7 |
st.set_page_config(
|
| 8 |
page_title="Code Assistant",
|
| 9 |
page_icon="🧠",
|
| 10 |
+
layout="wide"
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# ---------- Session State ----------
|
| 14 |
if "history" not in st.session_state:
|
| 15 |
st.session_state.history = []
|
| 16 |
|
| 17 |
+
if "datasets" not in st.session_state:
|
| 18 |
+
st.session_state.datasets = {}
|
| 19 |
+
|
| 20 |
# ---------- Core Logic ----------
|
| 21 |
@st.cache_data(show_spinner=False)
|
| 22 |
+
def generate_code(prompt: str, model_id: str, dataset_name: str | None) -> str:
|
| 23 |
"""
|
| 24 |
+
Placeholder for LLM inference.
|
|
|
|
| 25 |
"""
|
| 26 |
+
context = f"# Model: {model_id}\n"
|
| 27 |
+
if dataset_name:
|
| 28 |
+
context += f"# Dataset: {dataset_name}\n"
|
| 29 |
+
|
| 30 |
+
return f"""{context}
|
| 31 |
+
# Prompt:
|
| 32 |
+
# {prompt}
|
| 33 |
|
| 34 |
def example():
|
| 35 |
+
print("Generated using {model_id}")
|
| 36 |
"""
|
| 37 |
|
| 38 |
+
def handle_generation(prompt, model_id, dataset_name):
|
| 39 |
if not prompt.strip():
|
| 40 |
st.warning("Prompt cannot be empty.")
|
| 41 |
return
|
| 42 |
|
| 43 |
with st.spinner("Generating code…"):
|
| 44 |
+
output = generate_code(prompt, model_id, dataset_name)
|
| 45 |
|
| 46 |
st.session_state.history.append({
|
| 47 |
"timestamp": datetime.now().strftime("%H:%M:%S"),
|
| 48 |
+
"model": model_id,
|
| 49 |
+
"dataset": dataset_name,
|
| 50 |
"prompt": prompt,
|
| 51 |
+
"output": output
|
| 52 |
})
|
| 53 |
|
| 54 |
+
# ---------- Sidebar ----------
|
| 55 |
+
with st.sidebar:
|
| 56 |
+
st.header("⚙️ Configuration")
|
| 57 |
+
|
| 58 |
+
# Model selection (static for now)
|
| 59 |
+
model_id = st.selectbox(
|
| 60 |
+
"Model",
|
| 61 |
+
options=[
|
| 62 |
+
"mock-codegen-v1",
|
| 63 |
+
"llama-3-8b",
|
| 64 |
+
"mistral-7b",
|
| 65 |
+
],
|
| 66 |
+
help="Select the model used for code generation"
|
| 67 |
+
)
|
| 68 |
|
| 69 |
+
st.divider()
|
| 70 |
+
|
| 71 |
+
# Dataset upload
|
| 72 |
+
uploaded_file = st.file_uploader(
|
| 73 |
+
"Upload dataset",
|
| 74 |
+
type=["csv", "json", "txt"],
|
| 75 |
+
help="Optional dataset to condition generation"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if uploaded_file:
|
| 79 |
+
dataset_name = uploaded_file.name
|
| 80 |
|
| 81 |
+
if dataset_name not in st.session_state.datasets:
|
| 82 |
+
if uploaded_file.type == "text/csv":
|
| 83 |
+
df = pd.read_csv(uploaded_file)
|
| 84 |
+
elif uploaded_file.type == "application/json":
|
| 85 |
+
df = pd.read_json(uploaded_file)
|
| 86 |
+
else:
|
| 87 |
+
df = StringIO(uploaded_file.read().decode("utf-8")).read()
|
| 88 |
|
| 89 |
+
st.session_state.datasets[dataset_name] = df
|
|
|
|
| 90 |
|
| 91 |
+
st.success(f"Loaded: {dataset_name}")
|
|
|
|
| 92 |
|
| 93 |
+
# Dataset selection
|
| 94 |
+
dataset_name = st.selectbox(
|
| 95 |
+
"Active dataset",
|
| 96 |
+
options=[None] + list(st.session_state.datasets.keys()),
|
| 97 |
+
format_func=lambda x: "None" if x is None else x
|
| 98 |
+
)
|
| 99 |
|
| 100 |
+
# ---------- Main UI ----------
|
| 101 |
+
st.title("🧠 Code Assistant")
|
| 102 |
+
st.caption("Prompt → Model → (Optional) Dataset → Code")
|
| 103 |
+
|
| 104 |
+
prompt = st.text_area(
|
| 105 |
+
"Enter your coding prompt",
|
| 106 |
+
height=150,
|
| 107 |
+
placeholder="e.g. Generate a Python class that validates rows in the dataset"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
generate = st.button("Generate", type="primary")
|
| 111 |
+
|
| 112 |
+
if generate:
|
| 113 |
+
handle_generation(prompt, model_id, dataset_name)
|
| 114 |
|
| 115 |
# ---------- Output ----------
|
| 116 |
if st.session_state.history:
|
|
|
|
| 118 |
st.subheader("Generation History")
|
| 119 |
|
| 120 |
for item in reversed(st.session_state.history):
|
| 121 |
+
label = f"[{item['timestamp']}] {item['model']}"
|
| 122 |
+
if item["dataset"]:
|
| 123 |
+
label += f" | {item['dataset']}"
|
| 124 |
+
|
| 125 |
+
with st.expander(label):
|
| 126 |
+
st.markdown("**Prompt**")
|
| 127 |
+
st.write(item["prompt"])
|
| 128 |
+
|
| 129 |
+
st.markdown("**Generated Code**")
|
| 130 |
st.code(item["output"], language="python")
|