HaveAI commited on
Commit
ef9ef61
·
verified ·
1 Parent(s): 94b3823

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -29
app.py CHANGED
@@ -1,35 +1,23 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
 
5
- model_name = "microsoft/Phi-4-mini-instruct"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32)
 
8
 
9
- def chat(user_input, history=[]):
10
- messages = [{"role": "system", "content": "Ты полезный ассистент."}]
11
- for i, (q, a) in enumerate(history):
12
- messages.append({"role": "user", "content": q})
13
- messages.append({"role": "assistant", "content": a})
14
- messages.append({"role": "user", "content": user_input})
15
-
16
- inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
17
- outputs = model.generate(inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
18
- reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("assistant")[-1].strip()
19
-
20
- history.append((user_input, reply))
21
- return reply, history
22
 
23
- with gr.Blocks() as demo:
24
- gr.Markdown("# 🤖 Флэри чат (на Phi-4-mini)")
25
- chatbot = gr.Chatbot()
26
- msg = gr.Textbox(label="Ваш вопрос")
27
- state = gr.State([])
28
 
29
- def respond(message, history):
30
- reply, history = chat(message, history)
31
- return chatbot.update(value=history), history
 
 
32
 
33
- msg.submit(respond, [msg, state], [chatbot, state])
34
-
35
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
 
3
 
4
+ # Загружаем модель
5
+ model_id = "microsoft/Phi-4-mini-instruct"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
7
+ model = AutoModelForCausalLM.from_pretrained(model_id)
8
 
9
+ # Создаем пайплайн
10
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=200)
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Функция генерации текста
13
+ def chat(input_text):
14
+ result = pipe(input_text)[0]['generated_text']
15
+ return result
 
16
 
17
+ # Интерфейс Gradio
18
+ iface = gr.Interface(fn=chat,
19
+ inputs=gr.Textbox(lines=4, placeholder="Спроси меня что-нибудь..."),
20
+ outputs="text",
21
+ title="💬 Chat с Phi-4-mini-instruct")
22
 
23
+ iface.launch()