Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,20 +12,16 @@ st.set_page_config(
|
|
12 |
# Title of the app
|
13 |
st.title("💬 Qwen2.5-Coder Chat Interface")
|
14 |
|
15 |
-
# Initialize session state for messages
|
16 |
if 'messages' not in st.session_state:
|
17 |
st.session_state['messages'] = []
|
18 |
|
19 |
-
#
|
20 |
@st.cache_resource
|
21 |
def load_model():
|
22 |
-
model_name = "Qwen/Qwen2.5-Coder-32B-Instruct" # Replace with
|
23 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
24 |
-
model = AutoModelForCausalLM.from_pretrained(
|
25 |
-
model_name,
|
26 |
-
torch_dtype=torch.float16, # Use appropriate dtype for Hugging Face GPU environments
|
27 |
-
device_map='auto' # Automatically choose device (GPU/CPU)
|
28 |
-
)
|
29 |
return tokenizer, model
|
30 |
|
31 |
# Load tokenizer and model
|
@@ -33,10 +29,11 @@ with st.spinner("Loading model... This may take a while..."):
|
|
33 |
tokenizer, model = load_model()
|
34 |
|
35 |
# Function to generate model response
|
36 |
-
def generate_response(
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
with torch.no_grad():
|
41 |
outputs = model.generate(
|
42 |
inputs,
|
@@ -46,13 +43,14 @@ def generate_response(prompt, max_tokens=2048, temperature=0.7, top_p=0.9):
|
|
46 |
do_sample=True,
|
47 |
num_return_sequences=1
|
48 |
)
|
49 |
-
|
|
|
50 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
51 |
-
# Remove the prompt from the response
|
52 |
-
response = response[len(prompt):].strip()
|
53 |
-
return response
|
54 |
|
55 |
-
#
|
|
|
|
|
|
|
56 |
chat_col, sidebar_col = st.columns([4, 1])
|
57 |
|
58 |
with chat_col:
|
@@ -63,21 +61,23 @@ with chat_col:
|
|
63 |
else:
|
64 |
st.markdown(f"**Qwen2.5-Coder:** {message['content']}")
|
65 |
|
66 |
-
# Input area for user
|
67 |
with st.form(key='chat_form', clear_on_submit=True):
|
68 |
user_input = st.text_area("You:", height=100)
|
69 |
submit_button = st.form_submit_button(label='Send')
|
70 |
|
71 |
if submit_button and user_input:
|
72 |
-
# Append user message
|
73 |
st.session_state['messages'].append({'role': 'user', 'content': user_input})
|
74 |
-
|
75 |
-
# Generate and append model response
|
76 |
with st.spinner("Qwen2.5-Coder is typing..."):
|
77 |
-
response = generate_response(user_input
|
|
|
|
|
78 |
st.session_state['messages'].append({'role': 'assistant', 'content': response})
|
79 |
|
80 |
-
# Rerun to display new messages
|
81 |
st.experimental_rerun()
|
82 |
|
83 |
with sidebar_col:
|
@@ -86,7 +86,7 @@ with sidebar_col:
|
|
86 |
"Maximum Tokens",
|
87 |
min_value=512,
|
88 |
max_value=4096,
|
89 |
-
value=
|
90 |
step=256,
|
91 |
help="Set the maximum number of tokens for the model's response."
|
92 |
)
|
@@ -112,23 +112,3 @@ with sidebar_col:
|
|
112 |
if st.sidebar.button("Clear Chat"):
|
113 |
st.session_state['messages'] = []
|
114 |
st.experimental_rerun()
|
115 |
-
|
116 |
-
# Update the generate_response function to use sidebar settings dynamically
|
117 |
-
def generate_response(prompt):
|
118 |
-
inputs = tokenizer.encode(prompt, return_tensors='pt').to(model.device)
|
119 |
-
|
120 |
-
# Generate response
|
121 |
-
with torch.no_grad():
|
122 |
-
outputs = model.generate(
|
123 |
-
inputs,
|
124 |
-
max_length=max_tokens,
|
125 |
-
temperature=temperature,
|
126 |
-
top_p=top_p,
|
127 |
-
do_sample=True,
|
128 |
-
num_return_sequences=1
|
129 |
-
)
|
130 |
-
|
131 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
132 |
-
# Remove the prompt from the response
|
133 |
-
response = response[len(prompt):].strip()
|
134 |
-
return response
|
|
|
12 |
# Title of the app
|
13 |
st.title("💬 Qwen2.5-Coder Chat Interface")
|
14 |
|
15 |
+
# Initialize session state for messages (store conversation history)
|
16 |
if 'messages' not in st.session_state:
|
17 |
st.session_state['messages'] = []
|
18 |
|
19 |
+
# Load the model and tokenizer
|
20 |
@st.cache_resource
|
21 |
def load_model():
|
22 |
+
model_name = "Qwen/Qwen2.5-Coder-32B-Instruct" # Replace with the correct model path
|
23 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
24 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
|
|
|
|
|
|
|
|
25 |
return tokenizer, model
|
26 |
|
27 |
# Load tokenizer and model
|
|
|
29 |
tokenizer, model = load_model()
|
30 |
|
31 |
# Function to generate model response
|
32 |
+
def generate_response(user_input, max_tokens=150, temperature=0.7, top_p=0.9):
|
33 |
+
# Tokenize the user input
|
34 |
+
inputs = tokenizer.encode(user_input, return_tensors="pt").to(model.device)
|
35 |
+
|
36 |
+
# Generate a response
|
37 |
with torch.no_grad():
|
38 |
outputs = model.generate(
|
39 |
inputs,
|
|
|
43 |
do_sample=True,
|
44 |
num_return_sequences=1
|
45 |
)
|
46 |
+
|
47 |
+
# Decode the response
|
48 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
49 |
|
50 |
+
# Return the response without the input prompt
|
51 |
+
return response[len(user_input):].strip()
|
52 |
+
|
53 |
+
# Layout: Two columns for the main chat and sidebar
|
54 |
chat_col, sidebar_col = st.columns([4, 1])
|
55 |
|
56 |
with chat_col:
|
|
|
61 |
else:
|
62 |
st.markdown(f"**Qwen2.5-Coder:** {message['content']}")
|
63 |
|
64 |
+
# Input area for user message
|
65 |
with st.form(key='chat_form', clear_on_submit=True):
|
66 |
user_input = st.text_area("You:", height=100)
|
67 |
submit_button = st.form_submit_button(label='Send')
|
68 |
|
69 |
if submit_button and user_input:
|
70 |
+
# Append the user's message to the chat history
|
71 |
st.session_state['messages'].append({'role': 'user', 'content': user_input})
|
72 |
+
|
73 |
+
# Generate and append the model's response
|
74 |
with st.spinner("Qwen2.5-Coder is typing..."):
|
75 |
+
response = generate_response(user_input)
|
76 |
+
|
77 |
+
# Append the model's response to the chat history
|
78 |
st.session_state['messages'].append({'role': 'assistant', 'content': response})
|
79 |
|
80 |
+
# Rerun the app to display new messages
|
81 |
st.experimental_rerun()
|
82 |
|
83 |
with sidebar_col:
|
|
|
86 |
"Maximum Tokens",
|
87 |
min_value=512,
|
88 |
max_value=4096,
|
89 |
+
value=150,
|
90 |
step=256,
|
91 |
help="Set the maximum number of tokens for the model's response."
|
92 |
)
|
|
|
112 |
if st.sidebar.button("Clear Chat"):
|
113 |
st.session_state['messages'] = []
|
114 |
st.experimental_rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|