akhaliq HF Staff commited on
Commit
bf45d8f
Β·
verified Β·
1 Parent(s): 41aae02

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +246 -0
  2. requirements.txt +14 -0
app.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import os
5
+
6
+ # Model configuration
7
+ MODEL_NAME = "tencent/HY-MT1.5-1.8B"
8
+
9
+ # Global model and tokenizer instances
10
+ tokenizer = None
11
+ model = None
12
+
13
+ def load_model():
14
+ """Load the model and tokenizer lazily."""
15
+ global tokenizer, model
16
+ if tokenizer is None or model is None:
17
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
18
+ model = AutoModelForCausalLM.from_pretrained(
19
+ MODEL_NAME,
20
+ device_map="auto",
21
+ torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
22
+ )
23
+ return tokenizer, model
24
+
25
+ def generate_response(message: str, history: list, system_prompt: str = None) -> str:
26
+ """
27
+ Generate a response using the HY-MT1.5-1.8B model with chat template.
28
+
29
+ Args:
30
+ message: The user's input message
31
+ history: List of previous conversation messages
32
+ system_prompt: Optional system prompt for the conversation
33
+
34
+ Returns:
35
+ The model's generated response
36
+ """
37
+ try:
38
+ # Load model if not already loaded
39
+ tokenizer, model = load_model()
40
+
41
+ # Build messages list from history
42
+ messages = []
43
+
44
+ # Add system prompt if provided
45
+ if system_prompt:
46
+ messages.append({"role": "system", "content": system_prompt})
47
+
48
+ # Add conversation history
49
+ for msg in history:
50
+ messages.append(msg)
51
+
52
+ # Add current user message
53
+ messages.append({"role": "user", "content": message})
54
+
55
+ # Apply chat template and tokenize
56
+ tokenized_chat = tokenizer.apply_chat_template(
57
+ messages,
58
+ tokenize=True,
59
+ add_generation_prompt=True, # Add generation prompt for assistant turn
60
+ return_tensors="pt"
61
+ )
62
+
63
+ # Generate response
64
+ with torch.no_grad():
65
+ outputs = model.generate(
66
+ tokenized_chat.to(model.device),
67
+ max_new_tokens=1024,
68
+ temperature=0.7,
69
+ top_p=0.9,
70
+ do_sample=True,
71
+ pad_token_id=tokenizer.eos_token_id
72
+ )
73
+
74
+ # Decode the response
75
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
76
+
77
+ # Extract only the assistant's response (after the user's message)
78
+ if "assistant" in response:
79
+ response = response.split("assistant")[-1].strip()
80
+ elif "</s>" in response:
81
+ response = response.split("</s>")[-1].strip()
82
+
83
+ return response
84
+
85
+ except Exception as e:
86
+ return f"Error generating response: {str(e)}"
87
+
88
+ def create_conversation_message(role: str, content: str) -> dict:
89
+ """Create a message dictionary for the conversation."""
90
+ return {"role": role, "content": content}
91
+
92
+ # Create the Gradio 6 application
93
+ with gr.Blocks() as demo:
94
+ # Application header with branding
95
+ gr.Markdown(
96
+ """
97
+ # πŸ€– HY-MT1.5-1.8B Chatbot
98
+
99
+ A conversational AI powered by Tencent's HY-MT1.5-1.8B model.
100
+
101
+ ---
102
+
103
+ **Built with** [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
104
+ """,
105
+ elem_classes=["header"]
106
+ )
107
+
108
+ # Main chatbot interface
109
+ chat_interface = gr.ChatInterface(
110
+ fn=generate_response,
111
+ title="",
112
+ description="πŸ’¬ Start a conversation below! The model responds to your messages using the HY-MT1.5-1.8B chat template.",
113
+ chatbot=gr.Chatbot(
114
+ placeholder="πŸ’­ How can I help you today?",
115
+ height=500,
116
+ avatar_images=(
117
+ "https://huggingface.co/datasets/huggingface/avatars/resolve/main/user.png",
118
+ "https://huggingface.co/datasets/huggingface/avatars/resolve/main/tencent.png"
119
+ ),
120
+ show_copy_all_button=True,
121
+ feedback_options=("πŸ‘", "πŸ‘Ž")
122
+ ),
123
+ textbox=gr.MultimodalTextbox(
124
+ placeholder="Type your message here...",
125
+ lines=2,
126
+ max_lines=10,
127
+ submit_btn="Send ✈️",
128
+ stop_btn="Stop ⏹️"
129
+ ),
130
+ additional_inputs=[
131
+ gr.Textbox(
132
+ label="System Prompt (Optional)",
133
+ placeholder="You are a helpful assistant...",
134
+ lines=2,
135
+ max_lines=4
136
+ )
137
+ ],
138
+ additional_inputs_accordion=gr.Accordion(
139
+ label="βš™οΈ Advanced Settings",
140
+ open=False
141
+ ),
142
+ examples=[
143
+ ["Translate 'Hello, how are you?' into French."],
144
+ ["Explain quantum computing in simple terms."],
145
+ ["Write a short poem about the ocean."],
146
+ ["What are the benefits of exercise?"],
147
+ ["Help me plan a trip to Japan."]
148
+ ],
149
+ example_labels=["French Translation", "Quantum Computing", "Ocean Poem", "Exercise Benefits", "Japan Trip"],
150
+ submit_btn="Send ✈️",
151
+ clear_btn="Clear πŸ—‘οΈ",
152
+ autofocus=True,
153
+ fill_height=True,
154
+ api_visibility="public"
155
+ )
156
+
157
+ # Model information section
158
+ with gr.Accordion("πŸ“‹ Model Information", open=False):
159
+ gr.Markdown(f"""
160
+ ### Model Details
161
+
162
+ - **Model**: {MODEL_NAME}
163
+ - **Type**: Causal Language Model with Chat Template
164
+ - **Provider**: [Tencent](https://huggingface.co/tencent)
165
+
166
+ ### Capabilities
167
+
168
+ - πŸ“ Text generation and completion
169
+ - 🌍 Translation (supports multiple languages)
170
+ - πŸ’¬ Conversational AI
171
+ - πŸ“– Question answering
172
+ - ✍️ Creative writing
173
+
174
+ ### Usage Tips
175
+
176
+ - Be clear and specific in your requests
177
+ - For translations, specify the target language
178
+ - Use system prompts to customize behavior
179
+ - Model responds in the language of your query
180
+ """)
181
+
182
+ # Footer
183
+ gr.Markdown(
184
+ """
185
+ ---
186
+
187
+ *This application uses the HY-MT1.5-1.8B model from Hugging Face.
188
+ Responses are generated locally and are not reviewed.*
189
+ """,
190
+ elem_classes=["footer"]
191
+ )
192
+
193
+ # Launch the application with Gradio 6 configuration
194
+ demo.launch(
195
+ theme=gr.themes.Soft(
196
+ primary_hue="indigo",
197
+ secondary_hue="blue",
198
+ neutral_hue="slate",
199
+ font=gr.themes.GoogleFont("Inter"),
200
+ text_size="lg",
201
+ spacing_size="md",
202
+ radius_size="md"
203
+ ).set(
204
+ button_primary_background_fill="*primary_600",
205
+ button_primary_background_fill_hover="*primary_700",
206
+ block_title_text_weight="600"
207
+ ),
208
+ css="""
209
+ .header {
210
+ text-align: center;
211
+ padding: 20px;
212
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
213
+ border-radius: 12px;
214
+ margin-bottom: 20px;
215
+ }
216
+ .header h1 {
217
+ color: white !important;
218
+ margin-bottom: 10px;
219
+ }
220
+ .header a {
221
+ color: #ffd700;
222
+ font-weight: bold;
223
+ text-decoration: none;
224
+ }
225
+ .header a:hover {
226
+ text-decoration: underline;
227
+ }
228
+ .footer {
229
+ text-align: center;
230
+ color: #666;
231
+ font-size: 0.9em;
232
+ padding: 10px;
233
+ }
234
+ .gradio-container {
235
+ max-width: 1200px !important;
236
+ margin: 0 auto;
237
+ }
238
+ """,
239
+ footer_links=[
240
+ {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
241
+ {"label": "HY-MT1.5-1.8B", "url": "https://huggingface.co/tencent/HY-MT1.5-1.8B"},
242
+ {"label": "Tencent", "url": "https://huggingface.co/tencent"}
243
+ ],
244
+ height=800,
245
+ width="100%"
246
+ )
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio>=6.0
2
+ git+https://github.com/huggingface/transformers
3
+ torch
4
+ torchvision
5
+ torchaudio
6
+ accelerate
7
+ tokenizers
8
+ datasets
9
+ safetensors
10
+ sentencepiece
11
+ Pillow
12
+ requests
13
+ numpy
14
+ pandas