Repository navigation
Expand file tree
/
Copy pathst_codet5_small.py
More file actions
140 lines (119 loc) · 4.51 KB
/
Copy pathst_codet5_small.py
File metadata and controls
140 lines (119 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
107
108
109
110
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
138
139
140
import streamlit as st
import time
import sys
from gradio_client import Client
# Internal usage
import os
from time import sleep
# Model configuration for CodeT5-Small (880MB - Under 1.5GB requirement)
if "hf_model" not in st.session_state:
st.session_state.hf_model = "CodeT5-Small"
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
@st.cache_resource
def create_client():
yourHFtoken = os.getenv('HF_TOKEN', '') # Use environment variable for security
print(f'Loading lightweight model: {st.session_state.hf_model} (880MB)')
try:
client = Client("Salesforce/codet5-small", hf_token=yourHFtoken)
return client
except Exception as e:
st.error(f"Failed to load model: {e}")
return None
# FUNCTION TO LOG ALL CHAT MESSAGES INTO chathistory.txt
def writehistory(text):
with open('chathistory_codet5.txt', 'a', encoding='utf-8') as f:
f.write(text)
f.write('\n')
f.close()
#AVATARS
av_us = '🧑💻' # Developer avatar
av_ass = "🤖" # AI assistant avatar
### START STREAMLIT UI
st.set_page_config(
page_title="VARIABOT - CodeT5-Small",
page_icon="🤖",
layout="wide"
)
st.image('https://github.com/fabiomatricardi/ChatBOTMastery/raw/main/codet5logo.png', width=600)
st.markdown("### *CodeT5-Small: Lightweight Code Assistant (880MB)*", unsafe_allow_html=True)
st.markdown('---')
# Model information sidebar
with st.sidebar:
st.markdown("### 🤖 Model Information")
st.markdown(f"""
**Model**: CodeT5-Small
**Size**: 880MB ✅ (Under 1.5GB)
**Parameters**: 220M
**Specialization**: Code Generation
**Memory Usage**: Low
**Speed**: Fast
**Capabilities**:
- Python code generation
- Code completion
- Documentation generation
- Bug fixing suggestions
- Code explanation
""")
st.markdown("### 🔧 Settings")
max_tokens = st.slider("Max Tokens", 50, 500, 150)
temperature = st.slider("Temperature", 0.0, 1.0, 0.1)
# Create client
client = create_client()
if client is None:
st.error("⚠️ Model client failed to initialize. Please check your HuggingFace token.")
st.stop()
# Display chat messages from history on app rerun
for message in st.session_state.messages:
if message["role"] == "user":
with st.chat_message(message["role"], avatar=av_us):
st.markdown(message["content"])
else:
with st.chat_message(message["role"], avatar=av_ass):
st.markdown(message["content"])
# Accept user input
if myprompt := st.chat_input("Ask me to help with your code..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": myprompt})
# Display user message in chat message container
with st.chat_message("user", avatar=av_us):
st.markdown(myprompt)
usertext = f"user: {myprompt}"
writehistory(usertext)
# Display assistant response in chat message container
with st.chat_message("assistant", avatar=av_ass):
message_placeholder = st.empty()
full_response = ""
try:
# Show loading indicator for lightweight model
with st.spinner("🧠 CodeT5-Small is thinking..."):
res = client.submit(
myprompt,
max_length=max_tokens,
temperature=temperature,
api_name="/predict"
)
# Handle response
if res:
full_response = str(res)
message_placeholder.markdown(full_response)
else:
full_response = "I apologize, but I couldn't generate a response. Please try again."
message_placeholder.markdown(full_response)
except Exception as e:
full_response = f"Error: {str(e)}. The lightweight model may need a moment to initialize."
message_placeholder.markdown(full_response)
# Log assistant response
asstext = f"assistant: {full_response}"
writehistory(asstext)
st.session_state.messages.append({"role": "assistant", "content": full_response})
# Footer with model info
st.markdown("---")
st.markdown("""
<div style='text-align: center; color: #666;'>
🤖 <strong>VARIABOT - CodeT5-Small</strong><br>
Lightweight AI Code Assistant (880MB) | Efficient & Fast<br>
<em>Optimized for resource-constrained environments</em>
</div>
""", unsafe_allow_html=True)