Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,62 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
|
4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
try:
|
6 |
checkpoint = "Salesforce/codegen-350M-mono"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
@@ -10,31 +65,55 @@ except Exception as e:
|
|
10 |
st.error(f"Error loading model: {e}")
|
11 |
st.stop()
|
12 |
|
13 |
-
# Function to generate code
|
14 |
def generate_code(description):
|
15 |
prompt = f"Generate Python code for the following task: {description}\n"
|
16 |
inputs = tokenizer(prompt, return_tensors="pt")
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
)
|
24 |
code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
|
4 |
+
# Custom CSS for a Grok/ChatGPT-like look
|
5 |
+
st.markdown("""
|
6 |
+
<style>
|
7 |
+
.main { background-color: #f9f9f9; padding: 20px; }
|
8 |
+
.stTextArea textarea {
|
9 |
+
border: 1px solid #ddd;
|
10 |
+
border-radius: 8px;
|
11 |
+
padding: 10px;
|
12 |
+
font-family: 'Roboto', sans-serif;
|
13 |
+
font-size: 16px;
|
14 |
+
background-color: #fff;
|
15 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
16 |
+
}
|
17 |
+
.stButton button {
|
18 |
+
background-color: #4a90e2;
|
19 |
+
color: white;
|
20 |
+
border-radius: 8px;
|
21 |
+
padding: 10px 20px;
|
22 |
+
font-family: 'Roboto', sans-serif;
|
23 |
+
font-size: 14px;
|
24 |
+
}
|
25 |
+
.stButton button:hover {
|
26 |
+
background-color: #357abd;
|
27 |
+
}
|
28 |
+
.code-output {
|
29 |
+
background-color: #2b2b2b;
|
30 |
+
color: #f0f0f0;
|
31 |
+
padding: 15px;
|
32 |
+
border-radius: 8px;
|
33 |
+
font-family: 'Courier New', monospace;
|
34 |
+
font-size: 14px;
|
35 |
+
margin-top: 10px;
|
36 |
+
}
|
37 |
+
.title {
|
38 |
+
font-family: 'Roboto', sans-serif;
|
39 |
+
font-size: 28px;
|
40 |
+
font-weight: bold;
|
41 |
+
color: #333;
|
42 |
+
margin-bottom: 10px;
|
43 |
+
}
|
44 |
+
.subtitle {
|
45 |
+
font-family: 'Roboto', sans-serif;
|
46 |
+
font-size: 16px;
|
47 |
+
color: #666;
|
48 |
+
margin-bottom: 20px;
|
49 |
+
}
|
50 |
+
.chat-message {
|
51 |
+
font-family: 'Roboto', sans-serif;
|
52 |
+
font-size: 16px;
|
53 |
+
color: #333;
|
54 |
+
margin-bottom: 5px;
|
55 |
+
}
|
56 |
+
</style>
|
57 |
+
""", unsafe_allow_html=True)
|
58 |
+
|
59 |
+
# Load model and tokenizer
|
60 |
try:
|
61 |
checkpoint = "Salesforce/codegen-350M-mono"
|
62 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
|
|
65 |
st.error(f"Error loading model: {e}")
|
66 |
st.stop()
|
67 |
|
68 |
+
# Function to generate code
|
69 |
def generate_code(description):
|
70 |
prompt = f"Generate Python code for the following task: {description}\n"
|
71 |
inputs = tokenizer(prompt, return_tensors="pt")
|
72 |
+
outputs = model.generate(
|
73 |
+
**inputs,
|
74 |
+
max_length=500,
|
75 |
+
num_return_sequences=1,
|
76 |
+
pad_token_id=tokenizer.eos_token_id
|
77 |
+
)
|
|
|
78 |
code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
79 |
+
return code[len(prompt):].strip()
|
80 |
+
|
81 |
+
# Initialize chat history in session state
|
82 |
+
if "chat_history" not in st.session_state:
|
83 |
+
st.session_state.chat_history = []
|
84 |
+
|
85 |
+
# UI Layout
|
86 |
+
st.markdown('<div class="title">Code Generation Bot</div>', unsafe_allow_html=True)
|
87 |
+
st.markdown('<div class="subtitle">Describe your task, and I’ll generate Python code for you!</div>', unsafe_allow_html=True)
|
88 |
+
|
89 |
+
with st.container():
|
90 |
+
# Input area
|
91 |
+
description = st.text_area(
|
92 |
+
"Enter your description here",
|
93 |
+
placeholder="e.g., Write a function to calculate the factorial of a number",
|
94 |
+
height=150
|
95 |
+
)
|
96 |
+
|
97 |
+
col1, col2 = st.columns([1, 5])
|
98 |
+
with col1:
|
99 |
+
if st.button("Generate"):
|
100 |
+
if description.strip():
|
101 |
+
with st.spinner("Thinking..."):
|
102 |
+
generated_code = generate_code(description)
|
103 |
+
# Append to chat history
|
104 |
+
st.session_state.chat_history.append({"input": description, "output": generated_code})
|
105 |
+
else:
|
106 |
+
st.warning("Please enter a description first!")
|
107 |
+
with col2:
|
108 |
+
st.empty() # Spacer
|
109 |
+
|
110 |
+
# Display chat history
|
111 |
+
if st.session_state.chat_history:
|
112 |
+
st.write("### Chat History")
|
113 |
+
for chat in st.session_state.chat_history:
|
114 |
+
st.markdown(f'<div class="chat-message"><strong>You:</strong> {chat["input"]}</div>', unsafe_allow_html=True)
|
115 |
+
st.markdown(f'<div class="code-output">{chat["output"]}</div>', unsafe_allow_html=True)
|
116 |
+
st.markdown("---") # Separator for readability
|
117 |
+
|
118 |
+
# Optional tip at the bottom
|
119 |
+
st.info("Tip: Check the generated code for accuracy before using it!")
|