Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the model and tokenizer
|
| 6 |
+
model_name = "codellama/CodeLlama-7b-Python-hf"
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Function to generate Python code using the model
|
| 11 |
+
def generate_code(prompt):
|
| 12 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model.generate(
|
| 15 |
+
inputs['input_ids'],
|
| 16 |
+
max_length=200,
|
| 17 |
+
num_return_sequences=1,
|
| 18 |
+
temperature=0.7,
|
| 19 |
+
top_p=0.9,
|
| 20 |
+
top_k=50
|
| 21 |
+
)
|
| 22 |
+
code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
+
return code
|
| 24 |
+
|
| 25 |
+
# Streamlit app layout
|
| 26 |
+
st.set_page_config(page_title="Code Generator", layout="wide")
|
| 27 |
+
|
| 28 |
+
# Sidebar for history
|
| 29 |
+
with st.sidebar:
|
| 30 |
+
st.header("History")
|
| 31 |
+
history = st.empty() # This will be used to store the chat history
|
| 32 |
+
|
| 33 |
+
# Main area for the code generation interaction
|
| 34 |
+
st.title("CodeLlama Python Code Generator")
|
| 35 |
+
st.subheader("Ask me to write clean Python code!")
|
| 36 |
+
|
| 37 |
+
# Input section (bottom input box like ChatGPT)
|
| 38 |
+
user_input = st.text_input("Enter your prompt:", "")
|
| 39 |
+
|
| 40 |
+
if user_input:
|
| 41 |
+
# Generate Python code
|
| 42 |
+
prompt = f"Generate clean Python code for: {user_input}"
|
| 43 |
+
generated_code = generate_code(prompt)
|
| 44 |
+
|
| 45 |
+
# Show the output
|
| 46 |
+
st.code(generated_code, language="python")
|
| 47 |
+
|
| 48 |
+
# Update the chat history with the latest input and output
|
| 49 |
+
if "history" not in st.session_state:
|
| 50 |
+
st.session_state.history = []
|
| 51 |
+
|
| 52 |
+
st.session_state.history.append({
|
| 53 |
+
"user": user_input,
|
| 54 |
+
"response": generated_code
|
| 55 |
+
})
|
| 56 |
+
|
| 57 |
+
# Display history in the sidebar
|
| 58 |
+
if st.session_state.history:
|
| 59 |
+
history_text = ""
|
| 60 |
+
for entry in st.session_state.history:
|
| 61 |
+
history_text += f"User: {entry['user']}\nResponse: {entry['response']}\n\n"
|
| 62 |
+
history.text_area("Chat History", value=history_text, height=300)
|
| 63 |
+
|
| 64 |
+
# Clear history button (for new chat)
|
| 65 |
+
if st.button("Start New Chat"):
|
| 66 |
+
st.session_state.history = []
|
| 67 |
+
st.experimental_rerun()
|