Spaces:
Sleeping
Sleeping
Commit ·
36d5a1d
1
Parent(s): 6e0086a
feat: switch to GPT-2 model for token-free operation
Browse files- README.md +19 -13
- app.py +122 -72
- requirements.txt +4 -1
README.md
CHANGED
|
@@ -1,24 +1,30 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.41.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
short_description:
|
| 11 |
---
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
## Features
|
|
|
|
|
|
|
|
|
|
| 17 |
- No token required
|
| 18 |
-
-
|
| 19 |
-
- Simple interface
|
| 20 |
-
- Downloadable plans
|
| 21 |
|
| 22 |
-
##
|
| 23 |
-
-
|
| 24 |
-
-
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: AI Code & Analysis 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 AI assistant using CodeLlama-7b
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# AI Code & Analysis Assistant
|
| 14 |
+
|
| 15 |
+
Powered by CodeLlama-7b, offering:
|
| 16 |
+
- Professional Code Generation
|
| 17 |
+
- Technical Analysis
|
| 18 |
+
- Detailed Explanations
|
| 19 |
|
| 20 |
## Features
|
| 21 |
+
- State-of-the-art language model
|
| 22 |
+
- Advanced code completion
|
| 23 |
+
- Optimized for CPU
|
| 24 |
- No token required
|
| 25 |
+
- Memory efficient
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
## Best Practices
|
| 28 |
+
- Use clear prompts
|
| 29 |
+
- Specify programming language
|
| 30 |
+
- Include context for better results
|
app.py
CHANGED
|
@@ -2,23 +2,31 @@ import streamlit as st
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
import gc
|
| 5 |
-
from
|
|
|
|
| 6 |
|
| 7 |
@st.cache_resource
|
| 8 |
def load_model():
|
| 9 |
try:
|
| 10 |
-
model_id = "
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
tokenizer.pad_token = tokenizer.eos_token
|
| 13 |
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
model_id,
|
| 16 |
torch_dtype=torch.float32,
|
| 17 |
-
low_cpu_mem_usage=True
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
model.eval()
|
| 21 |
-
torch.set_num_threads(
|
| 22 |
gc.collect()
|
| 23 |
return model, tokenizer
|
| 24 |
|
|
@@ -26,95 +34,137 @@ def load_model():
|
|
| 26 |
st.error(f"Error loading model: {str(e)}")
|
| 27 |
st.stop()
|
| 28 |
|
| 29 |
-
|
| 30 |
-
def generate_diet_plan(health_condition: str, dietary_restrictions: str, cache_key: str):
|
| 31 |
model, tokenizer = load_model()
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
Include:
|
| 37 |
-
1. Recommended Foods:
|
| 38 |
-
2. Foods to Avoid:
|
| 39 |
-
3. Meal Schedule:
|
| 40 |
-
4. Special Notes:
|
| 41 |
-
"""
|
| 42 |
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
outputs = model.generate(
|
| 48 |
inputs["input_ids"],
|
| 49 |
-
max_length=
|
| 50 |
temperature=0.7,
|
| 51 |
-
top_p=0.
|
|
|
|
| 52 |
repetition_penalty=1.2,
|
|
|
|
|
|
|
| 53 |
pad_token_id=tokenizer.eos_token_id
|
| 54 |
)
|
| 55 |
|
| 56 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 57 |
-
return response.
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
-
return "
|
| 61 |
-
|
| 62 |
-
# Streamlit interface
|
| 63 |
-
st.title("🏥 Health Diet Planner")
|
| 64 |
|
| 65 |
-
st.
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
"
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
).strip()
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
if
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
# Clear cache button
|
| 109 |
if st.sidebar.button("Clear Cache"):
|
| 110 |
-
generate_diet_plan.cache_clear()
|
| 111 |
st.cache_resource.clear()
|
| 112 |
st.success("Cache cleared!")
|
| 113 |
|
| 114 |
-
st.markdown("""
|
| 115 |
-
###
|
| 116 |
-
-
|
| 117 |
-
-
|
| 118 |
-
-
|
| 119 |
-
- Update your health condition details for more accurate recommendations
|
| 120 |
""")
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
import gc
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
@st.cache_resource
|
| 9 |
def load_model():
|
| 10 |
try:
|
| 11 |
+
model_id = "CodeLlama-7b-Instruct-hf"
|
| 12 |
+
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 14 |
+
model_id,
|
| 15 |
+
use_fast=True,
|
| 16 |
+
trust_remote_code=True
|
| 17 |
+
)
|
| 18 |
tokenizer.pad_token = tokenizer.eos_token
|
| 19 |
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
model_id,
|
| 22 |
torch_dtype=torch.float32,
|
| 23 |
+
low_cpu_mem_usage=True,
|
| 24 |
+
device_map="cpu",
|
| 25 |
+
max_memory={'cpu': '16GB'}
|
| 26 |
+
)
|
| 27 |
|
| 28 |
model.eval()
|
| 29 |
+
torch.set_num_threads(8) # Increased threads for better performance
|
| 30 |
gc.collect()
|
| 31 |
return model, tokenizer
|
| 32 |
|
|
|
|
| 34 |
st.error(f"Error loading model: {str(e)}")
|
| 35 |
st.stop()
|
| 36 |
|
| 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 |
+
# Format prompt based on type
|
| 45 |
+
if image:
|
| 46 |
+
formatted_prompt = f"<s>[INST] {system_prompt}\nAnalyze this image: {image}\n\n{prompt} [/INST]"
|
| 47 |
+
else:
|
| 48 |
+
formatted_prompt = f"<s>[INST] {system_prompt}\n{prompt} [/INST]"
|
| 49 |
|
| 50 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt", padding=True)
|
| 51 |
+
|
| 52 |
+
with torch.inference_mode():
|
| 53 |
outputs = model.generate(
|
| 54 |
inputs["input_ids"],
|
| 55 |
+
max_length=2048,
|
| 56 |
temperature=0.7,
|
| 57 |
+
top_p=0.95,
|
| 58 |
+
top_k=50,
|
| 59 |
repetition_penalty=1.2,
|
| 60 |
+
do_sample=True,
|
| 61 |
+
num_return_sequences=1,
|
| 62 |
pad_token_id=tokenizer.eos_token_id
|
| 63 |
)
|
| 64 |
|
| 65 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 66 |
+
return response.split('[/INST]')[-1].strip()
|
| 67 |
|
| 68 |
except Exception as e:
|
| 69 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
st.title("🤖 Multi-Purpose AI Assistant")
|
| 72 |
+
st.write("Generate code, analyze images, or get detailed explanations")
|
| 73 |
|
| 74 |
+
# Sidebar for task selection
|
| 75 |
+
task = st.sidebar.selectbox(
|
| 76 |
+
"Choose Task",
|
| 77 |
+
["Generate Code", "Analyze Image", "Explain Concept"]
|
| 78 |
+
)
|
|
|
|
| 79 |
|
| 80 |
+
# Add language categories and options
|
| 81 |
+
PROGRAMMING_LANGUAGES = {
|
| 82 |
+
"Web Development": ["HTML", "CSS", "JavaScript", "TypeScript", "PHP"],
|
| 83 |
+
"Backend": ["Python", "Java", "C#", "Ruby", "Go", "Node.js"],
|
| 84 |
+
"Data & ML": ["Python", "R", "SQL", "Julia"],
|
| 85 |
+
"Mobile": ["Swift", "Kotlin", "Java", "React Native"],
|
| 86 |
+
"System": ["C", "C++", "Rust", "Shell"]
|
| 87 |
+
}
|
| 88 |
|
| 89 |
+
if task == "Generate Code":
|
| 90 |
+
# Enhanced language selection
|
| 91 |
+
category = st.selectbox(
|
| 92 |
+
"Select Category",
|
| 93 |
+
list(PROGRAMMING_LANGUAGES.keys())
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
language = st.selectbox(
|
| 97 |
+
"Programming Language",
|
| 98 |
+
PROGRAMMING_LANGUAGES[category],
|
| 99 |
+
help="Choose the programming language for your code"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
template = st.selectbox(
|
| 103 |
+
"Code Template",
|
| 104 |
+
["Basic Script", "Function", "Class", "Full Program"],
|
| 105 |
+
help="Select the type of code structure you want"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
code_prompt = st.text_area(
|
| 109 |
+
"Describe what you want to create:",
|
| 110 |
+
placeholder=f"Example: Create a {language} {template.lower()} that..."
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
if st.button("Generate Code"):
|
| 114 |
+
if code_prompt:
|
| 115 |
+
with st.spinner(f"Generating {language} code..."):
|
| 116 |
+
prompt = f"""Write {language} code for: {code_prompt}
|
| 117 |
+
Type: {template}
|
| 118 |
+
Requirements:
|
| 119 |
+
- Clean and efficient code
|
| 120 |
+
- Follow best practices
|
| 121 |
+
- Include necessary imports
|
| 122 |
+
- Provide only code without explanation
|
| 123 |
+
"""
|
| 124 |
+
response = generate_response(prompt)
|
| 125 |
+
st.code(response, language=language.lower())
|
| 126 |
|
| 127 |
+
# Add copy button
|
| 128 |
+
st.button(
|
| 129 |
+
"📋 Copy Code",
|
| 130 |
+
help="Copy code to clipboard",
|
| 131 |
+
on_click=lambda: st.write(response)
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
elif task == "Analyze Image":
|
| 135 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 136 |
+
if uploaded_file:
|
| 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 |
+
else: # Explain Concept
|
| 152 |
+
concept = st.text_input("Enter the concept you want to understand:")
|
| 153 |
+
if st.button("Explain"):
|
| 154 |
+
if concept:
|
| 155 |
+
with st.spinner("Generating explanation..."):
|
| 156 |
+
prompt = f"Explain in detail: {concept}"
|
| 157 |
+
response = generate_response(prompt)
|
| 158 |
+
st.markdown(response)
|
| 159 |
|
| 160 |
+
# Clear cache button
|
| 161 |
if st.sidebar.button("Clear Cache"):
|
|
|
|
| 162 |
st.cache_resource.clear()
|
| 163 |
st.success("Cache cleared!")
|
| 164 |
|
| 165 |
+
st.sidebar.markdown("""
|
| 166 |
+
### Tips:
|
| 167 |
+
- Be specific in your prompts
|
| 168 |
+
- For code, mention language and functionality
|
| 169 |
+
- For images, upload clear pictures
|
|
|
|
| 170 |
""")
|
requirements.txt
CHANGED
|
@@ -2,6 +2,9 @@
|
|
| 2 |
# streamlit is already pre-installed
|
| 3 |
streamlit>=1.41.1
|
| 4 |
torch>=2.0.0
|
| 5 |
-
transformers>=4.
|
| 6 |
accelerate>=0.21.0
|
|
|
|
|
|
|
|
|
|
| 7 |
scikit-learn
|
|
|
|
| 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
|
| 10 |
scikit-learn
|