Locon213 commited on
Commit
fb4c3dc
·
verified ·
1 Parent(s): 2032e93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py CHANGED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from peft import PeftModel
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
3
+ import gradio as gr
4
+
5
+ # Загрузка модели и токенизатора
6
+ base_model = AutoModelForCausalLM.from_pretrained(
7
+ "Qwen/Qwen2.5-0.5B-Instruct",
8
+ device_map="auto"
9
+ )
10
+ model = PeftModel.from_pretrained(base_model, "Locon213/ThinkLite")
11
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
12
+
13
+ # Конфигурация генерации
14
+ generation_config = GenerationConfig(
15
+ temperature=0.7,
16
+ top_p=0.9,
17
+ top_k=50,
18
+ max_new_tokens=512,
19
+ repetition_penalty=1.1,
20
+ do_sample=True
21
+ )
22
+
23
+ def format_prompt(message, history):
24
+ prompt = ""
25
+ for user_msg, bot_msg in history:
26
+ prompt += f"<<<USER>>> {user_msg}\n<<<ASSISTANT>>> {bot_msg}\n"
27
+ prompt += f"<<<USER>>> {message}\n<<<ASSISTANT>>>"
28
+ return prompt
29
+
30
+ def generate_response(message, history):
31
+ # Форматируем промпт с историей чата
32
+ formatted_prompt = format_prompt(message, history)
33
+
34
+ # Токенизация и генерация
35
+ inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
36
+ outputs = model.generate(
37
+ **inputs,
38
+ generation_config=generation_config,
39
+ pad_token_id=tokenizer.eos_token_id
40
+ )
41
+
42
+ # Декодирование и извлечение ответа
43
+ response = tokenizer.decode(outputs[0][len(inputs.input_ids[0]):], skip_special_tokens=True)
44
+
45
+ return response.strip()
46
+
47
+ # Создание чат-интерфейса
48
+ chat_interface = gr.ChatInterface(
49
+ fn=generate_response,
50
+ examples=[
51
+ "Объясни квантовую запутанность простыми словами",
52
+ "Как научиться программировать?",
53
+ "Напиши стихотворение про ИИ"
54
+ ],
55
+ title="ThinkLite Chat",
56
+ description="Общайтесь с ThinkLite - адаптированной версией Qwen2.5-0.5B-Instruct",
57
+ theme="soft"
58
+ )
59
+
60
+ if __name__ == "__main__":
61
+ chat_interface.launch()