Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Set Streamlit page configuration
|
6 |
+
st.set_page_config(
|
7 |
+
page_title="Qwen2.5-Coder Chat",
|
8 |
+
page_icon="💬",
|
9 |
+
layout="wide",
|
10 |
+
)
|
11 |
+
|
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 |
+
# Function to load the model
|
20 |
+
@st.cache_resource
|
21 |
+
def load_model():
|
22 |
+
model_name = "Qwen/Qwen2.5-Coder-32B-Instruct" # Replace with your model path or name
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
24 |
+
model = AutoModelForCausalLM.from_pretrained(
|
25 |
+
model_name,
|
26 |
+
torch_dtype=torch.float16, # Use appropriate dtype
|
27 |
+
device_map='auto' # Automatically choose device (GPU/CPU)
|
28 |
+
)
|
29 |
+
return tokenizer, model
|
30 |
+
|
31 |
+
# Load tokenizer and model
|
32 |
+
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(prompt, max_tokens=2048):
|
37 |
+
inputs = tokenizer.encode(prompt, return_tensors='pt').to(model.device)
|
38 |
+
|
39 |
+
# Generate response
|
40 |
+
with torch.no_grad():
|
41 |
+
outputs = model.generate(
|
42 |
+
inputs,
|
43 |
+
max_length=max_tokens,
|
44 |
+
temperature=0.7, # Adjust for creativity
|
45 |
+
top_p=0.9, # Nucleus sampling
|
46 |
+
do_sample=True, # Enable sampling
|
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 |
+
# Layout: Two columns, main chat and sidebar
|
56 |
+
chat_col, sidebar_col = st.columns([4, 1])
|
57 |
+
|
58 |
+
with chat_col:
|
59 |
+
# Display chat messages
|
60 |
+
for message in st.session_state['messages']:
|
61 |
+
if message['role'] == 'user':
|
62 |
+
st.markdown(f"**You:** {message['content']}")
|
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, max_tokens=2048)
|
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:
|
84 |
+
st.sidebar.header("Settings")
|
85 |
+
max_tokens = st.sidebar.slider(
|
86 |
+
"Maximum Tokens",
|
87 |
+
min_value=512,
|
88 |
+
max_value=4096,
|
89 |
+
value=2048,
|
90 |
+
step=256,
|
91 |
+
help="Set the maximum number of tokens for the model's response."
|
92 |
+
)
|
93 |
+
|
94 |
+
temperature = st.sidebar.slider(
|
95 |
+
"Temperature",
|
96 |
+
min_value=0.1,
|
97 |
+
max_value=1.0,
|
98 |
+
value=0.7,
|
99 |
+
step=0.1,
|
100 |
+
help="Controls the randomness of the model's output."
|
101 |
+
)
|
102 |
+
|
103 |
+
top_p = st.sidebar.slider(
|
104 |
+
"Top-p (Nucleus Sampling)",
|
105 |
+
min_value=0.1,
|
106 |
+
max_value=1.0,
|
107 |
+
value=0.9,
|
108 |
+
step=0.1,
|
109 |
+
help="Controls the diversity of the model's output."
|
110 |
+
)
|
111 |
+
|
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
|
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
|