Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,56 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
else:
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
st.error(f"An error occurred: {e}")
|
27 |
-
|
28 |
-
# Handling Code Optimization:
|
29 |
-
if task_type == "Code Optimization":
|
30 |
-
st.info("Paste your Python code for optimization recommendations.")
|
31 |
-
user_code = st.text_area("Paste your Python code", height=200)
|
32 |
-
if st.button("Optimize Code"):
|
33 |
-
if user_code.strip() == "":
|
34 |
-
st.warning("Please paste valid Python code to optimize.")
|
35 |
-
else:
|
36 |
-
with st.spinner("Analyzing and optimizing..."):
|
37 |
-
try:
|
38 |
-
optimization_prompt = f"Optimize the following Python code:\n\n{user_code}"
|
39 |
-
output = nlp_pipeline(optimization_prompt)
|
40 |
-
optimized_code = output[0]["generated_text"]
|
41 |
-
st.subheader("Optimized Code")
|
42 |
-
st.code(optimized_code)
|
43 |
-
except Exception as e:
|
44 |
-
st.error(f"An error occurred while optimizing code: {e}")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
# Load the model and tokenizer
|
5 |
+
@st.cache_resource
|
6 |
+
def load_model():
|
7 |
+
model_name = "replit/replit-code-v1-3b" # Replace with your fine-tuned model if applicable
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
10 |
+
return model, tokenizer
|
11 |
+
|
12 |
+
model, tokenizer = load_model()
|
13 |
+
|
14 |
+
# App title and description
|
15 |
+
st.title("Replit-code-v1-3b Code Assistant 📊")
|
16 |
+
st.markdown("""
|
17 |
+
This application allows you to interact with the **Replit-code-v1-3b** large language model.
|
18 |
+
You can use it to generate, debug, or optimize code snippets.
|
19 |
+
Simply provide a prompt, and the model will respond with suggestions!
|
20 |
+
""")
|
21 |
+
|
22 |
+
# User input
|
23 |
+
st.header("Enter Your Prompt")
|
24 |
+
prompt = st.text_area("Describe your coding task or paste your code for debugging:")
|
25 |
+
|
26 |
+
# Temperature and max length controls
|
27 |
+
st.sidebar.header("Model Settings")
|
28 |
+
temperature = st.sidebar.slider("Temperature (Creativity)", 0.0, 1.0, 0.7)
|
29 |
+
max_length = st.sidebar.slider("Max Response Length", 50, 500, 200)
|
30 |
+
|
31 |
+
# Submit button
|
32 |
+
if st.button("Generate Response"):
|
33 |
+
if prompt.strip():
|
34 |
+
with st.spinner("Generating response..."):
|
35 |
+
# Tokenize and generate response
|
36 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
37 |
+
outputs = model.generate(
|
38 |
+
inputs.input_ids,
|
39 |
+
max_length=max_length,
|
40 |
+
temperature=temperature,
|
41 |
+
pad_token_id=tokenizer.eos_token_id
|
42 |
+
)
|
43 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
44 |
+
|
45 |
+
# Display response
|
46 |
+
st.subheader("Generated Code/Response")
|
47 |
+
st.code(response, language="python")
|
48 |
else:
|
49 |
+
st.warning("Please enter a prompt to generate a response.")
|
50 |
+
|
51 |
+
# Footer
|
52 |
+
st.markdown("---")
|
53 |
+
st.markdown("""
|
54 |
+
**Replit-code-v1-3b Code Assistant**
|
55 |
+
Built with [Streamlit](https://streamlit.io/) and the [Hugging Face Transformers Library](https://huggingface.co/docs/transformers).
|
56 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|