|
|
import streamlit as st |
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
import torch |
|
|
|
|
|
|
|
|
model_name = "codellama/CodeLlama-7b-Python-hf" |
|
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
|
|
|
|
|
|
def generate_code(prompt): |
|
|
inputs = tokenizer(prompt, return_tensors="pt") |
|
|
with torch.no_grad(): |
|
|
outputs = model.generate( |
|
|
inputs['input_ids'], |
|
|
max_length=200, |
|
|
num_return_sequences=1, |
|
|
temperature=0.7, |
|
|
top_p=0.9, |
|
|
top_k=50 |
|
|
) |
|
|
code = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
return code |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Code Generator", layout="wide") |
|
|
|
|
|
|
|
|
with st.sidebar: |
|
|
st.header("History") |
|
|
history = st.empty() |
|
|
|
|
|
|
|
|
st.title("CodeLlama Python Code Generator") |
|
|
st.subheader("Ask me to write clean Python code!") |
|
|
|
|
|
|
|
|
user_input = st.text_input("Enter your prompt:", "") |
|
|
|
|
|
if user_input: |
|
|
|
|
|
prompt = f"Generate clean Python code for: {user_input}" |
|
|
generated_code = generate_code(prompt) |
|
|
|
|
|
|
|
|
st.code(generated_code, language="python") |
|
|
|
|
|
|
|
|
if "history" not in st.session_state: |
|
|
st.session_state.history = [] |
|
|
|
|
|
st.session_state.history.append({ |
|
|
"user": user_input, |
|
|
"response": generated_code |
|
|
}) |
|
|
|
|
|
|
|
|
if st.session_state.history: |
|
|
history_text = "" |
|
|
for entry in st.session_state.history: |
|
|
history_text += f"User: {entry['user']}\nResponse: {entry['response']}\n\n" |
|
|
history.text_area("Chat History", value=history_text, height=300) |
|
|
|
|
|
|
|
|
if st.button("Start New Chat"): |
|
|
st.session_state.history = [] |
|
|
st.experimental_rerun() |
|
|
|