Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +13 -105
src/streamlit_app.py
CHANGED
|
@@ -1,67 +1,15 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
|
| 5 |
-
# ==============================
|
| 6 |
-
# PAGE CONFIG
|
| 7 |
-
# ==============================
|
| 8 |
-
st.set_page_config(page_title="π» AI Code Generator", layout="wide")
|
| 9 |
-
|
| 10 |
-
# ==============================
|
| 11 |
-
# LOAD MODEL
|
| 12 |
-
# ==============================
|
| 13 |
-
@st.cache_resource
|
| 14 |
-
def load_model():
|
| 15 |
-
model_name = "deepseek-ai/deepseek-coder-6.7b-instruct" # π₯ BEST MODEL
|
| 16 |
-
|
| 17 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
-
|
| 19 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
-
model_name,
|
| 21 |
-
torch_dtype=torch.float32,
|
| 22 |
-
device_map="auto" # auto uses GPU if available
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
return tokenizer, model
|
| 26 |
-
|
| 27 |
-
tokenizer, model = load_model()
|
| 28 |
-
|
| 29 |
-
# ==============================
|
| 30 |
-
# CLEAN OUTPUT
|
| 31 |
-
# ==============================
|
| 32 |
-
def clean_code(code):
|
| 33 |
-
code = code.strip()
|
| 34 |
-
|
| 35 |
-
remove_words = [
|
| 36 |
-
"Explanation:",
|
| 37 |
-
"Here is the code:",
|
| 38 |
-
"Output:",
|
| 39 |
-
"Answer:"
|
| 40 |
-
]
|
| 41 |
-
|
| 42 |
-
for word in remove_words:
|
| 43 |
-
code = code.replace(word, "")
|
| 44 |
-
|
| 45 |
-
# Remove markdown formatting
|
| 46 |
-
code = code.replace("```python", "").replace("```", "")
|
| 47 |
-
|
| 48 |
-
return code.strip()
|
| 49 |
-
|
| 50 |
-
# ==============================
|
| 51 |
-
# GENERATE CODE
|
| 52 |
-
# ==============================
|
| 53 |
def generate_code(prompt, language):
|
| 54 |
|
| 55 |
full_prompt = f"""
|
| 56 |
-
You are an expert {language}
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
-
|
| 62 |
-
-
|
| 63 |
- Proper syntax
|
| 64 |
-
- Complete
|
| 65 |
|
| 66 |
Task:
|
| 67 |
{prompt}
|
|
@@ -74,59 +22,19 @@ Code:
|
|
| 74 |
with torch.no_grad():
|
| 75 |
outputs = model.generate(
|
| 76 |
**inputs,
|
| 77 |
-
max_new_tokens=
|
| 78 |
-
do_sample=True,
|
| 79 |
-
temperature=0.2,
|
| 80 |
-
top_p=0.9,
|
| 81 |
-
repetition_penalty=1.1,
|
| 82 |
eos_token_id=tokenizer.eos_token_id,
|
| 83 |
pad_token_id=tokenizer.eos_token_id
|
| 84 |
)
|
| 85 |
|
| 86 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 87 |
|
|
|
|
| 88 |
if "Code:" in result:
|
| 89 |
result = result.split("Code:")[-1]
|
| 90 |
|
| 91 |
-
return clean_code(result)
|
| 92 |
-
|
| 93 |
-
# ==============================
|
| 94 |
-
# SESSION STATE (CHAT HISTORY)
|
| 95 |
-
# ==============================
|
| 96 |
-
if "history" not in st.session_state:
|
| 97 |
-
st.session_state.history = []
|
| 98 |
-
|
| 99 |
-
# ==============================
|
| 100 |
-
# UI
|
| 101 |
-
# ==============================
|
| 102 |
-
st.title("π» AI Code Generator (Advanced)")
|
| 103 |
-
|
| 104 |
-
col1, col2 = st.columns(2)
|
| 105 |
-
|
| 106 |
-
with col1:
|
| 107 |
-
user_prompt = st.text_area("Describe your task", height=200)
|
| 108 |
-
|
| 109 |
-
with col2:
|
| 110 |
-
language = st.selectbox(
|
| 111 |
-
"Select Programming Language",
|
| 112 |
-
["Python", "JavaScript", "SQL", "Java", "C++", "HTML", "CSS"]
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
# ==============================
|
| 116 |
-
# GENERATE BUTTON
|
| 117 |
-
# ==============================
|
| 118 |
-
if st.button("Generate Code"):
|
| 119 |
-
if not user_prompt.strip():
|
| 120 |
-
st.warning("β οΈ Please enter a task")
|
| 121 |
-
else:
|
| 122 |
-
with st.spinner("β‘ Generating high-quality code..."):
|
| 123 |
-
code = generate_code(user_prompt, language)
|
| 124 |
-
|
| 125 |
-
st.session_state.history.append((user_prompt, code))
|
| 126 |
-
|
| 127 |
-
# ==============================
|
| 128 |
-
# DISPLAY HISTORY
|
| 129 |
-
# ==============================
|
| 130 |
-
for q, c in reversed(st.session_state.history):
|
| 131 |
-
st.markdown(f"### π§ Task:\n{q}")
|
| 132 |
-
st.code(c, language=language.lower())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def generate_code(prompt, language):
|
| 2 |
|
| 3 |
full_prompt = f"""
|
| 4 |
+
You are an expert {language} programmer.
|
| 5 |
|
| 6 |
+
Write clean, correct, and complete code.
|
| 7 |
|
| 8 |
+
Requirements:
|
| 9 |
+
- No explanations
|
| 10 |
+
- Only code
|
| 11 |
- Proper syntax
|
| 12 |
+
- Complete function/program
|
| 13 |
|
| 14 |
Task:
|
| 15 |
{prompt}
|
|
|
|
| 22 |
with torch.no_grad():
|
| 23 |
outputs = model.generate(
|
| 24 |
**inputs,
|
| 25 |
+
max_new_tokens=300,
|
| 26 |
+
do_sample=True, # β
IMPORTANT
|
| 27 |
+
temperature=0.2, # β
slight creativity
|
| 28 |
+
top_p=0.9, # β
better coherence
|
| 29 |
+
repetition_penalty=1.1, # β
avoid loops
|
| 30 |
eos_token_id=tokenizer.eos_token_id,
|
| 31 |
pad_token_id=tokenizer.eos_token_id
|
| 32 |
)
|
| 33 |
|
| 34 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
|
| 36 |
+
# Remove prompt part safely
|
| 37 |
if "Code:" in result:
|
| 38 |
result = result.split("Code:")[-1]
|
| 39 |
|
| 40 |
+
return clean_code(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|