Garush65 commited on
Commit
b00c98c
·
verified ·
1 Parent(s): 34707a3

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +77 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Загрузка модели SmolLM3-3B
6
+ print("🚀 Загрузка SmolLM3-3B...")
7
+ model_name = "HuggingFaceTB/SmolLM3-3B"
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ model_name,
12
+ torch_dtype=torch.float32,
13
+ low_cpu_mem_usage=True
14
+ )
15
+
16
+ print("✅ Модель загружена!")
17
+
18
+ def chat(message, history):
19
+ """Обработка сообщений в чате"""
20
+ # Собираем историю разговора
21
+ conversation = ""
22
+ for user_msg, bot_msg in history:
23
+ conversation += f"User: {user_msg}\nAssistant: {bot_msg}\n"
24
+ conversation += f"User: {message}\nAssistant:"
25
+
26
+ # Генерируем ответ
27
+ inputs = tokenizer(conversation, return_tensors="pt", truncation=True, max_length=2048)
28
+
29
+ with torch.no_grad():
30
+ outputs = model.generate(
31
+ **inputs,
32
+ max_new_tokens=300,
33
+ temperature=0.7,
34
+ do_sample=True,
35
+ top_p=0.9,
36
+ repetition_penalty=1.1
37
+ )
38
+
39
+ # Декодируем ответ
40
+ full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
41
+
42
+ # Извлекаем только новый ответ ассистента
43
+ if "Assistant:" in full_response:
44
+ response = full_response.split("Assistant:")[-1].strip()
45
+ else:
46
+ response = full_response.strip()
47
+
48
+ return response
49
+
50
+ # Создаём интерфейс
51
+ demo = gr.ChatInterface(
52
+ fn=chat,
53
+ title="🌍 SmolLM3-3B Multilingual Chat",
54
+ description="""
55
+ **Мультиязычный AI чат-бот на базе SmolLM3-3B (3 миллиарда параметров)**
56
+
57
+ Поддерживаемые языки:
58
+ 🇬🇧 English | 🇷🇺 Русский | 🇪🇸 Español | 🇫🇷 Français | 🇩🇪 Deutsch | 🇮🇹 Italiano | 🇵🇹 Português
59
+
60
+ Полностью бесплатный и без ограничений! 🚀
61
+ """,
62
+ examples=[
63
+ "Привет! Расскажи о себе",
64
+ "Hello! Tell me about yourself",
65
+ "¿Hola! ¿Qué puedes hacer?",
66
+ "Bonjour! Que peux-tu faire?",
67
+ "Hallo! Was kannst du tun?",
68
+ ],
69
+ cache_examples=False,
70
+ retry_btn="🔄 Повторить",
71
+ undo_btn="↩️ Отменить",
72
+ clear_btn="🗑️ Очистить",
73
+ )
74
+
75
+ # Запуск
76
+ if __name__ == "__main__":
77
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers>=4.35.0
2
+ torch>=2.0.0
3
+ gradio>=4.0.0
4
+ accelerate>=0.24.0
5
+ sentencepiece>=0.1.99