Lyon28 commited on
Commit
55a5521
Β·
verified Β·
1 Parent(s): 20d8357

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +328 -0
app.py ADDED
@@ -0,0 +1,328 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import (
4
+ AutoModelForCausalLM, AutoTokenizer, AutoModelForSequenceClassification,
5
+ T5ForConditionalGeneration, T5Tokenizer, pipeline
6
+ )
7
+ import warnings
8
+ warnings.filterwarnings("ignore")
9
+
10
+ class MultiModelHub:
11
+ def __init__(self):
12
+ self.models = {}
13
+ self.tokenizers = {}
14
+ self.pipelines = {}
15
+ self.model_configs = {
16
+ # Text Generation Models
17
+ "GPT-2 Indonesia": {
18
+ "model_name": "Lyon28/GPT-2",
19
+ "type": "text-generation",
20
+ "description": "GPT-2 fine-tuned untuk bahasa Indonesia"
21
+ },
22
+ "Tinny Llama": {
23
+ "model_name": "Lyon28/Tinny-Llama",
24
+ "type": "text-generation",
25
+ "description": "Compact language model untuk chat"
26
+ },
27
+ "Pythia": {
28
+ "model_name": "Lyon28/Pythia",
29
+ "type": "text-generation",
30
+ "description": "Pythia model untuk text generation"
31
+ },
32
+ "GPT-Neo": {
33
+ "model_name": "Lyon28/GPT-Neo",
34
+ "type": "text-generation",
35
+ "description": "GPT-Neo untuk creative writing"
36
+ },
37
+ "Distil GPT-2": {
38
+ "model_name": "Lyon28/Distil_GPT-2",
39
+ "type": "text-generation",
40
+ "description": "Lightweight GPT-2 variant"
41
+ },
42
+ "GPT-2 Tinny": {
43
+ "model_name": "Lyon28/GPT-2-Tinny",
44
+ "type": "text-generation",
45
+ "description": "Compact GPT-2 model"
46
+ },
47
+
48
+ # Classification Models
49
+ "BERT Tinny": {
50
+ "model_name": "Lyon28/Bert-Tinny",
51
+ "type": "text-classification",
52
+ "description": "BERT untuk klasifikasi teks"
53
+ },
54
+ "ALBERT Base": {
55
+ "model_name": "Lyon28/Albert-Base-V2",
56
+ "type": "text-classification",
57
+ "description": "ALBERT untuk analisis sentimen"
58
+ },
59
+ "DistilBERT": {
60
+ "model_name": "Lyon28/Distilbert-Base-Uncased",
61
+ "type": "text-classification",
62
+ "description": "Efficient BERT untuk classification"
63
+ },
64
+ "ELECTRA Small": {
65
+ "model_name": "Lyon28/Electra-Small",
66
+ "type": "text-classification",
67
+ "description": "ELECTRA untuk text understanding"
68
+ },
69
+
70
+ # Text-to-Text Model
71
+ "T5 Small": {
72
+ "model_name": "Lyon28/T5-Small",
73
+ "type": "text2text-generation",
74
+ "description": "T5 untuk berbagai NLP tasks"
75
+ }
76
+ }
77
+
78
+ def load_model(self, model_key):
79
+ """Load model on-demand untuk menghemat memory"""
80
+ if model_key in self.pipelines:
81
+ return self.pipelines[model_key]
82
+
83
+ try:
84
+ config = self.model_configs[model_key]
85
+ model_name = config["model_name"]
86
+ model_type = config["type"]
87
+
88
+ # Load pipeline berdasarkan type
89
+ if model_type == "text-generation":
90
+ pipe = pipeline(
91
+ "text-generation",
92
+ model=model_name,
93
+ tokenizer=model_name,
94
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
95
+ device_map="auto" if torch.cuda.is_available() else None
96
+ )
97
+ elif model_type == "text-classification":
98
+ pipe = pipeline(
99
+ "text-classification",
100
+ model=model_name,
101
+ tokenizer=model_name
102
+ )
103
+ elif model_type == "text2text-generation":
104
+ pipe = pipeline(
105
+ "text2text-generation",
106
+ model=model_name,
107
+ tokenizer=model_name
108
+ )
109
+ else:
110
+ raise ValueError(f"Unsupported model type: {model_type}")
111
+
112
+ self.pipelines[model_key] = pipe
113
+ return pipe
114
+
115
+ except Exception as e:
116
+ return f"Error loading model {model_key}: {str(e)}"
117
+
118
+ def generate_text(self, model_key, prompt, max_length=100, temperature=0.7, top_p=0.9):
119
+ """Generate text menggunakan model yang dipilih"""
120
+ try:
121
+ pipe = self.load_model(model_key)
122
+ if isinstance(pipe, str): # Error message
123
+ return pipe
124
+
125
+ config = self.model_configs[model_key]
126
+
127
+ if config["type"] == "text-generation":
128
+ result = pipe(
129
+ prompt,
130
+ max_length=max_length,
131
+ temperature=temperature,
132
+ top_p=top_p,
133
+ do_sample=True,
134
+ pad_token_id=pipe.tokenizer.eos_token_id
135
+ )
136
+ generated_text = result[0]['generated_text']
137
+ # Remove prompt dari output
138
+ if generated_text.startswith(prompt):
139
+ generated_text = generated_text[len(prompt):].strip()
140
+ return generated_text
141
+
142
+ elif config["type"] == "text-classification":
143
+ result = pipe(prompt)
144
+ return f"Label: {result[0]['label']}, Score: {result[0]['score']:.4f}"
145
+
146
+ elif config["type"] == "text2text-generation":
147
+ result = pipe(prompt, max_length=max_length)
148
+ return result[0]['generated_text']
149
+
150
+ except Exception as e:
151
+ return f"Error generating text: {str(e)}"
152
+
153
+ def get_model_info(self, model_key):
154
+ """Get informasi model"""
155
+ config = self.model_configs[model_key]
156
+ return f"**{model_key}**\n\nType: {config['type']}\n\nDescription: {config['description']}"
157
+
158
+ # Initialize hub
159
+ hub = MultiModelHub()
160
+
161
+ def chat_interface(model_choice, user_input, max_length, temperature, top_p, history):
162
+ """Main chat interface"""
163
+ if not user_input.strip():
164
+ return history, ""
165
+
166
+ # Generate response
167
+ response = hub.generate_text(
168
+ model_choice,
169
+ user_input,
170
+ max_length=int(max_length),
171
+ temperature=temperature,
172
+ top_p=top_p
173
+ )
174
+
175
+ # Update history
176
+ history.append([user_input, response])
177
+
178
+ return history, ""
179
+
180
+ def get_model_description(model_choice):
181
+ """Update model description"""
182
+ return hub.get_model_info(model_choice)
183
+
184
+ # Gradio Interface
185
+ with gr.Blocks(title="Lyon28 Multi-Model Hub", theme=gr.themes.Soft()) as demo:
186
+ gr.Markdown(
187
+ """
188
+ # πŸ€– Lyon28 Multi-Model Hub
189
+
190
+ Deploy dan test semua 11 models Lyon28 dalam satu interface.
191
+ Pilih model, atur parameter, dan mulai chat!
192
+ """
193
+ )
194
+
195
+ with gr.Row():
196
+ with gr.Column(scale=1):
197
+ # Model Selection
198
+ model_dropdown = gr.Dropdown(
199
+ choices=list(hub.model_configs.keys()),
200
+ value="GPT-2 Indonesia",
201
+ label="Select Model",
202
+ info="Choose which model to use"
203
+ )
204
+
205
+ # Model Info
206
+ model_info = gr.Markdown(
207
+ hub.get_model_info("GPT-2 Indonesia"),
208
+ label="Model Information"
209
+ )
210
+
211
+ # Parameters
212
+ gr.Markdown("### Generation Parameters")
213
+ max_length_slider = gr.Slider(
214
+ minimum=20,
215
+ maximum=500,
216
+ value=100,
217
+ step=10,
218
+ label="Max Length",
219
+ info="Maximum response length"
220
+ )
221
+
222
+ temperature_slider = gr.Slider(
223
+ minimum=0.1,
224
+ maximum=2.0,
225
+ value=0.7,
226
+ step=0.1,
227
+ label="Temperature",
228
+ info="Creativity level (higher = more creative)"
229
+ )
230
+
231
+ top_p_slider = gr.Slider(
232
+ minimum=0.1,
233
+ maximum=1.0,
234
+ value=0.9,
235
+ step=0.05,
236
+ label="Top-p",
237
+ info="Nucleus sampling parameter"
238
+ )
239
+
240
+ with gr.Column(scale=2):
241
+ # Chat Interface
242
+ chatbot = gr.Chatbot(
243
+ label="Chat with Model",
244
+ height=400,
245
+ show_label=True
246
+ )
247
+
248
+ user_input = gr.Textbox(
249
+ placeholder="Type your message here...",
250
+ label="Your Message",
251
+ lines=2
252
+ )
253
+
254
+ with gr.Row():
255
+ send_btn = gr.Button("Send", variant="primary")
256
+ clear_btn = gr.Button("Clear Chat", variant="secondary")
257
+
258
+ # Example Prompts
259
+ gr.Markdown("### πŸ’‘ Example Prompts")
260
+ example_prompts = gr.Examples(
261
+ examples=[
262
+ ["Ceritakan tentang Indonesia"],
263
+ ["What is artificial intelligence?"],
264
+ ["Write a Python function to sort a list"],
265
+ ["Explain quantum computing in simple terms"],
266
+ ["Create a short story about robots"],
267
+ ],
268
+ inputs=user_input,
269
+ label="Click to use example prompts"
270
+ )
271
+
272
+ # Event Handlers
273
+ model_dropdown.change(
274
+ fn=get_model_description,
275
+ inputs=[model_dropdown],
276
+ outputs=[model_info]
277
+ )
278
+
279
+ send_btn.click(
280
+ fn=chat_interface,
281
+ inputs=[model_dropdown, user_input, max_length_slider, temperature_slider, top_p_slider, chatbot],
282
+ outputs=[chatbot, user_input]
283
+ )
284
+
285
+ user_input.submit(
286
+ fn=chat_interface,
287
+ inputs=[model_dropdown, user_input, max_length_slider, temperature_slider, top_p_slider, chatbot],
288
+ outputs=[chatbot, user_input]
289
+ )
290
+
291
+ clear_btn.click(
292
+ fn=lambda: ([], ""),
293
+ outputs=[chatbot, user_input]
294
+ )
295
+
296
+ # Footer
297
+ with demo:
298
+ gr.Markdown(
299
+ """
300
+ ---
301
+
302
+ ### πŸš€ Features:
303
+ - **11 Models**: Akses semua model Lyon28 dalam satu tempat
304
+ - **Multiple Types**: Text generation, classification, dan text2text
305
+ - **Configurable**: Adjust temperature, top-p, dan max length
306
+ - **Memory Efficient**: Models loaded on-demand
307
+ - **API Ready**: Gradio auto-generates API endpoints
308
+
309
+ ### πŸ“‘ API Usage:
310
+ ```python
311
+ import requests
312
+
313
+ response = requests.post(
314
+ "https://your-space-name.hf.space/api/predict",
315
+ json={"data": ["GPT-2 Indonesia", "Hello world", 100, 0.7, 0.9, []]}
316
+ )
317
+ ```
318
+
319
+ **Built by Lyon28** πŸ”₯
320
+ """
321
+ )
322
+
323
+ if __name__ == "__main__":
324
+ demo.launch(
325
+ share=True,
326
+ server_name="0.0.0.0",
327
+ server_port=7860
328
+ )