Spaces:
Sleeping
Sleeping
Commit ยท
53e6fb8
1
Parent(s): 36d5a1d
feat: switch to deepseek model for token-free operation
Browse files- README.md +23 -17
- app.py +182 -108
- requirements.txt +8 -2
README.md
CHANGED
|
@@ -1,30 +1,36 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.41.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
short_description: Advanced
|
| 11 |
---
|
| 12 |
|
| 13 |
-
#
|
| 14 |
|
| 15 |
-
|
| 16 |
-
-
|
| 17 |
-
-
|
| 18 |
-
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
## Features
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
-
-
|
| 24 |
-
-
|
| 25 |
-
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
##
|
| 28 |
-
- Use
|
| 29 |
-
- Specify
|
| 30 |
-
-
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: DeepSeek Code Assistant
|
| 3 |
+
emoji: ๐
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.41.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
short_description: Advanced Code Generation with Enhanced UI
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# DeepSeek Code Assistant Pro
|
| 14 |
|
| 15 |
+
A powerful code generation tool with enhanced UI features:
|
| 16 |
+
- Modern, responsive interface
|
| 17 |
+
- Advanced code customization
|
| 18 |
+
- Multiple template options
|
| 19 |
+
- Syntax highlighting
|
| 20 |
+
- One-click code copying
|
| 21 |
+
- Code downloading
|
| 22 |
|
| 23 |
## Features
|
| 24 |
+
- Interactive code generation
|
| 25 |
+
- Real-time preview
|
| 26 |
+
- Multiple programming languages
|
| 27 |
+
- Custom templates
|
| 28 |
+
- Error handling options
|
| 29 |
+
- Automatic commenting
|
| 30 |
+
- Test generation
|
| 31 |
|
| 32 |
+
## Pro Tips
|
| 33 |
+
- Use detailed descriptions
|
| 34 |
+
- Specify requirements clearly
|
| 35 |
+
- Select appropriate templates
|
| 36 |
+
- Enable useful options
|
app.py
CHANGED
|
@@ -5,15 +5,59 @@ import gc
|
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@st.cache_resource
|
| 9 |
def load_model():
|
| 10 |
try:
|
| 11 |
-
model_id = "
|
| 12 |
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 14 |
model_id,
|
| 15 |
-
|
| 16 |
-
|
| 17 |
)
|
| 18 |
tokenizer.pad_token = tokenizer.eos_token
|
| 19 |
|
|
@@ -26,7 +70,7 @@ def load_model():
|
|
| 26 |
)
|
| 27 |
|
| 28 |
model.eval()
|
| 29 |
-
torch.set_num_threads(8)
|
| 30 |
gc.collect()
|
| 31 |
return model, tokenizer
|
| 32 |
|
|
@@ -37,23 +81,27 @@ def load_model():
|
|
| 37 |
def generate_response(prompt, image=None):
|
| 38 |
model, tokenizer = load_model()
|
| 39 |
|
| 40 |
-
system_prompt = """You are a helpful AI assistant skilled in coding, image analysis, and explanations.
|
| 41 |
-
Provide clear, concise, and accurate responses."""
|
| 42 |
-
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
if
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
with torch.inference_mode():
|
| 53 |
outputs = model.generate(
|
| 54 |
inputs["input_ids"],
|
| 55 |
-
max_length=2048,
|
| 56 |
-
temperature=0.
|
| 57 |
top_p=0.95,
|
| 58 |
top_k=50,
|
| 59 |
repetition_penalty=1.2,
|
|
@@ -63,108 +111,134 @@ def generate_response(prompt, image=None):
|
|
| 63 |
)
|
| 64 |
|
| 65 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
except Exception as e:
|
| 69 |
return f"Error: {str(e)}"
|
| 70 |
|
| 71 |
-
|
| 72 |
-
st.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
category = st.selectbox(
|
| 92 |
-
"Select Category",
|
| 93 |
-
list(PROGRAMMING_LANGUAGES.keys())
|
| 94 |
-
)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
"
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
"
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
|
| 108 |
-
|
| 109 |
-
"Describe
|
| 110 |
-
placeholder=
|
|
|
|
| 111 |
)
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
image = Image.open(uploaded_file)
|
| 138 |
-
st.image(image, caption="Uploaded Image")
|
| 139 |
-
|
| 140 |
-
analysis_type = st.selectbox(
|
| 141 |
-
"What would you like to know?",
|
| 142 |
-
["Describe Image", "Technical Analysis", "Extract Text"]
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
if st.button("Analyze"):
|
| 146 |
-
with st.spinner("Analyzing image..."):
|
| 147 |
-
prompt = f"Analyze this image for {analysis_type}:"
|
| 148 |
-
response = generate_response(prompt, image)
|
| 149 |
-
st.write(response)
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
if
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
- Be specific in your prompts
|
| 168 |
-
- For code, mention language and functionality
|
| 169 |
-
- For images, upload clear pictures
|
| 170 |
-
""")
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
|
| 8 |
+
# Set page configuration
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="DeepSeek Coding Assistant",
|
| 11 |
+
page_icon="๐",
|
| 12 |
+
layout="wide",
|
| 13 |
+
initial_sidebar_state="expanded"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Add custom CSS
|
| 17 |
+
st.markdown("""
|
| 18 |
+
<style>
|
| 19 |
+
.main {
|
| 20 |
+
padding: 2rem;
|
| 21 |
+
}
|
| 22 |
+
.stButton button {
|
| 23 |
+
width: 100%;
|
| 24 |
+
border-radius: 5px;
|
| 25 |
+
height: 3em;
|
| 26 |
+
background-color: #4CAF50;
|
| 27 |
+
color: white;
|
| 28 |
+
}
|
| 29 |
+
.stTextInput > div > div > input {
|
| 30 |
+
border-radius: 5px;
|
| 31 |
+
}
|
| 32 |
+
.stSelectbox > div > div > select {
|
| 33 |
+
border-radius: 5px;
|
| 34 |
+
}
|
| 35 |
+
.output-container {
|
| 36 |
+
background-color: #f0f2f6;
|
| 37 |
+
padding: 20px;
|
| 38 |
+
border-radius: 10px;
|
| 39 |
+
margin: 10px 0;
|
| 40 |
+
}
|
| 41 |
+
.success-message {
|
| 42 |
+
color: #4CAF50;
|
| 43 |
+
font-weight: bold;
|
| 44 |
+
}
|
| 45 |
+
.error-message {
|
| 46 |
+
color: #ff4444;
|
| 47 |
+
font-weight: bold;
|
| 48 |
+
}
|
| 49 |
+
</style>
|
| 50 |
+
""", unsafe_allow_html=True)
|
| 51 |
+
|
| 52 |
@st.cache_resource
|
| 53 |
def load_model():
|
| 54 |
try:
|
| 55 |
+
model_id = "deepseek-ai/deepseek-coder-1.3b-base"
|
| 56 |
|
| 57 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 58 |
model_id,
|
| 59 |
+
trust_remote_code=True,
|
| 60 |
+
padding_side='left'
|
| 61 |
)
|
| 62 |
tokenizer.pad_token = tokenizer.eos_token
|
| 63 |
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
model.eval()
|
| 73 |
+
torch.set_num_threads(8)
|
| 74 |
gc.collect()
|
| 75 |
return model, tokenizer
|
| 76 |
|
|
|
|
| 81 |
def generate_response(prompt, image=None):
|
| 82 |
model, tokenizer = load_model()
|
| 83 |
|
|
|
|
|
|
|
|
|
|
| 84 |
try:
|
| 85 |
+
code_prompt = f"""Write professional code based on the given requirements.
|
| 86 |
+
Language: {prompt.split('code for:')[0] if 'code for:' in prompt else 'any'}
|
| 87 |
+
Requirements: {prompt}
|
| 88 |
+
|
| 89 |
+
Here's the implementation:
|
| 90 |
+
```"""
|
| 91 |
+
|
| 92 |
+
inputs = tokenizer(
|
| 93 |
+
code_prompt,
|
| 94 |
+
return_tensors="pt",
|
| 95 |
+
padding=True,
|
| 96 |
+
max_length=1024,
|
| 97 |
+
truncation=True
|
| 98 |
+
)
|
| 99 |
|
| 100 |
with torch.inference_mode():
|
| 101 |
outputs = model.generate(
|
| 102 |
inputs["input_ids"],
|
| 103 |
+
max_length=2048, # Increased for longer code
|
| 104 |
+
temperature=0.5, # More focused
|
| 105 |
top_p=0.95,
|
| 106 |
top_k=50,
|
| 107 |
repetition_penalty=1.2,
|
|
|
|
| 111 |
)
|
| 112 |
|
| 113 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 114 |
+
# Clean up response
|
| 115 |
+
code = response.split("```")[1] if "```" in response else response
|
| 116 |
+
return code.strip()
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
return f"Error: {str(e)}"
|
| 120 |
|
| 121 |
+
def create_sidebar():
|
| 122 |
+
with st.sidebar:
|
| 123 |
+
st.image("https://raw.githubusercontent.com/streamlit/streamlit/develop/examples/streamlit_app_example.png",
|
| 124 |
+
width=100)
|
| 125 |
+
st.title("๐ ๏ธ Settings")
|
| 126 |
+
|
| 127 |
+
task = st.selectbox(
|
| 128 |
+
"Select Task",
|
| 129 |
+
["๐ป Code Generation", "๐ผ๏ธ Image Analysis", "๐ Concept Explanation"]
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
st.markdown("---")
|
| 133 |
+
|
| 134 |
+
if st.button("โป๏ธ Clear Cache", use_container_width=True):
|
| 135 |
+
st.cache_resource.clear()
|
| 136 |
+
st.success("Cache cleared successfully!")
|
| 137 |
+
|
| 138 |
+
st.markdown("""
|
| 139 |
+
### ๐ Pro Tips
|
| 140 |
+
- Use detailed descriptions
|
| 141 |
+
- Specify edge cases
|
| 142 |
+
- Include example inputs/outputs
|
| 143 |
+
""")
|
| 144 |
+
|
| 145 |
+
return task.split()[1] # Return without emoji
|
| 146 |
|
| 147 |
+
def code_generation_ui():
|
| 148 |
+
col1, col2 = st.columns([2, 1])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
with col1:
|
| 151 |
+
st.markdown("### ๐ Code Requirements")
|
| 152 |
+
category = st.selectbox(
|
| 153 |
+
"Domain",
|
| 154 |
+
list(PROGRAMMING_LANGUAGES.keys()),
|
| 155 |
+
help="Select the type of application"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
language = st.selectbox(
|
| 159 |
+
"Language",
|
| 160 |
+
PROGRAMMING_LANGUAGES[category],
|
| 161 |
+
help="Choose programming language"
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
template = st.selectbox(
|
| 165 |
+
"Template",
|
| 166 |
+
["Basic Script", "Function", "Class", "Full Program", "API", "Database"],
|
| 167 |
+
help="Select code structure"
|
| 168 |
+
)
|
| 169 |
|
| 170 |
+
with col2:
|
| 171 |
+
st.markdown("### โ๏ธ Options")
|
| 172 |
+
add_comments = st.checkbox("Add Comments", value=True)
|
| 173 |
+
include_tests = st.checkbox("Include Tests")
|
| 174 |
+
error_handling = st.checkbox("Error Handling")
|
| 175 |
|
| 176 |
+
prompt = st.text_area(
|
| 177 |
+
"Describe Your Code Requirements",
|
| 178 |
+
placeholder="Example: Create a function that takes a list of numbers and returns the sum of even numbers...",
|
| 179 |
+
height=150
|
| 180 |
)
|
| 181 |
|
| 182 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
| 183 |
+
with col2:
|
| 184 |
+
generate = st.button("๐ Generate Code", use_container_width=True)
|
| 185 |
+
|
| 186 |
+
if generate and prompt:
|
| 187 |
+
with st.spinner("๐ฎ Generating your code..."):
|
| 188 |
+
options = {
|
| 189 |
+
"comments": add_comments,
|
| 190 |
+
"tests": include_tests,
|
| 191 |
+
"error_handling": error_handling
|
| 192 |
+
}
|
| 193 |
+
code = generate_enhanced_response(prompt, language, template, options)
|
| 194 |
+
|
| 195 |
+
st.markdown("### ๐ Generated Code")
|
| 196 |
+
with st.expander("Show Code", expanded=True):
|
| 197 |
+
st.code(code, language=language.lower())
|
| 198 |
|
| 199 |
+
col1, col2 = st.columns([1, 1])
|
| 200 |
+
with col1:
|
| 201 |
+
st.download_button(
|
| 202 |
+
"๐พ Download Code",
|
| 203 |
+
code,
|
| 204 |
+
file_name=f"generated_code.{language.lower()}",
|
| 205 |
+
mime="text/plain"
|
| 206 |
+
)
|
| 207 |
+
with col2:
|
| 208 |
+
st.button("๐ Copy to Clipboard")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
def main():
|
| 211 |
+
task = create_sidebar()
|
| 212 |
+
|
| 213 |
+
st.markdown("# ๐ DeepSeek Coding Assistant")
|
| 214 |
+
st.markdown("---")
|
| 215 |
+
|
| 216 |
+
if task == "Code Generation":
|
| 217 |
+
code_generation_ui()
|
| 218 |
+
elif task == "Image Analysis":
|
| 219 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 220 |
+
if uploaded_file:
|
| 221 |
+
image = Image.open(uploaded_file)
|
| 222 |
+
st.image(image, caption="Uploaded Image")
|
| 223 |
+
|
| 224 |
+
analysis_type = st.selectbox(
|
| 225 |
+
"What would you like to know?",
|
| 226 |
+
["Describe Image", "Technical Analysis", "Extract Text"]
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
if st.button("Analyze"):
|
| 230 |
+
with st.spinner("Analyzing image..."):
|
| 231 |
+
prompt = f"Analyze this image for {analysis_type}:"
|
| 232 |
+
response = generate_response(prompt, image)
|
| 233 |
+
st.write(response)
|
| 234 |
+
else:
|
| 235 |
+
concept = st.text_input("Enter the concept you want to understand:")
|
| 236 |
+
if st.button("Explain"):
|
| 237 |
+
if concept:
|
| 238 |
+
with st.spinner("Generating explanation..."):
|
| 239 |
+
prompt = f"Explain in detail: {concept}"
|
| 240 |
+
response = generate_response(prompt)
|
| 241 |
+
st.markdown(response)
|
| 242 |
|
| 243 |
+
if __name__ == "__main__":
|
| 244 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,9 +1,15 @@
|
|
| 1 |
-
#
|
| 2 |
-
# streamlit is already pre-installed
|
| 3 |
streamlit>=1.41.1
|
| 4 |
torch>=2.0.0
|
| 5 |
transformers>=4.33.0
|
| 6 |
accelerate>=0.21.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
sentencepiece>=0.1.99
|
| 8 |
Pillow>=9.0.0
|
| 9 |
einops>=0.6.1
|
|
|
|
| 1 |
+
# Core dependencies
|
|
|
|
| 2 |
streamlit>=1.41.1
|
| 3 |
torch>=2.0.0
|
| 4 |
transformers>=4.33.0
|
| 5 |
accelerate>=0.21.0
|
| 6 |
+
|
| 7 |
+
# UI enhancements
|
| 8 |
+
streamlit-ace>=0.1.1
|
| 9 |
+
streamlit-extras>=0.3.0
|
| 10 |
+
streamlit-code-editor>=0.1.6
|
| 11 |
+
|
| 12 |
+
# Model dependencies
|
| 13 |
sentencepiece>=0.1.99
|
| 14 |
Pillow>=9.0.0
|
| 15 |
einops>=0.6.1
|