Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +89 -34
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,95 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
+
# ==============================
|
| 6 |
+
# π HIDE STREAMLIT MENU
|
| 7 |
+
# ==============================
|
| 8 |
+
st.markdown("""
|
| 9 |
+
<style>
|
| 10 |
+
#MainMenu {visibility: hidden;}
|
| 11 |
+
header {visibility: hidden;}
|
| 12 |
+
footer {visibility: hidden;}
|
| 13 |
+
.stDeployButton {display:none;}
|
| 14 |
+
</style>
|
| 15 |
+
""", unsafe_allow_html=True)
|
| 16 |
+
|
| 17 |
+
# ==============================
|
| 18 |
+
# PAGE CONFIG
|
| 19 |
+
# ==============================
|
| 20 |
+
st.set_page_config(page_title="π» AI Code Generator", layout="wide")
|
| 21 |
+
|
| 22 |
+
# ==============================
|
| 23 |
+
# LOAD MODEL
|
| 24 |
+
# ==============================
|
| 25 |
+
@st.cache_resource
|
| 26 |
+
def load_model():
|
| 27 |
+
model_name = "codellama/CodeLlama-7b-Instruct-hf"
|
| 28 |
+
|
| 29 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 30 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 31 |
+
model_name,
|
| 32 |
+
torch_dtype=torch.float16,
|
| 33 |
+
device_map="auto"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
return tokenizer, model
|
| 37 |
+
|
| 38 |
+
tokenizer, model = load_model()
|
| 39 |
+
|
| 40 |
+
# ==============================
|
| 41 |
+
# CODE GENERATION FUNCTION
|
| 42 |
+
# ==============================
|
| 43 |
+
def generate_code(prompt, language):
|
| 44 |
+
|
| 45 |
+
full_prompt = f"""
|
| 46 |
+
You are an expert {language} developer.
|
| 47 |
+
|
| 48 |
+
Write clean, optimized, production-ready code.
|
| 49 |
|
| 50 |
+
Task:
|
| 51 |
+
{prompt}
|
|
|
|
| 52 |
|
| 53 |
+
Rules:
|
| 54 |
+
- Only return code
|
| 55 |
+
- No explanation
|
| 56 |
"""
|
| 57 |
|
| 58 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 59 |
+
|
| 60 |
+
outputs = model.generate(
|
| 61 |
+
**inputs,
|
| 62 |
+
max_new_tokens=300,
|
| 63 |
+
temperature=0.2,
|
| 64 |
+
top_p=0.9
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 68 |
+
|
| 69 |
+
return result.replace(full_prompt, "").strip()
|
| 70 |
+
|
| 71 |
+
# ==============================
|
| 72 |
+
# UI
|
| 73 |
+
# ==============================
|
| 74 |
+
st.title("π» AI Code Generator")
|
| 75 |
+
|
| 76 |
+
col1, col2 = st.columns(2)
|
| 77 |
+
|
| 78 |
+
with col1:
|
| 79 |
+
user_prompt = st.text_area("Describe your task", height=200)
|
| 80 |
+
|
| 81 |
+
with col2:
|
| 82 |
+
language = st.selectbox(
|
| 83 |
+
"Select Programming Language",
|
| 84 |
+
["Python", "JavaScript", "SQL", "Java", "C++", "HTML", "CSS"]
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if st.button("Generate Code"):
|
| 88 |
+
if not user_prompt.strip():
|
| 89 |
+
st.warning("Please enter a task")
|
| 90 |
+
else:
|
| 91 |
+
with st.spinner("Generating code..."):
|
| 92 |
+
code = generate_code(user_prompt, language)
|
| 93 |
+
|
| 94 |
+
st.success("β
Generated Code")
|
| 95 |
+
st.code(code, language=language.lower())
|