HaveAI commited on
Commit
99f9b2d
·
verified ·
1 Parent(s): 359afbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -1,18 +1,36 @@
1
  # app.py
2
  import gradio as gr
 
3
 
4
- def echo(message, history):
 
 
 
 
 
 
 
 
 
 
 
5
  history = history or []
6
- reply = f"Ты написал: {message}"
7
- history.append((message, reply))
8
- return history, history
 
 
 
 
 
 
9
 
10
- demo = gr.ChatInterface(
11
- fn=echo,
12
  type="messages",
13
- examples=["Привет", "Как дела?", "Расскажи шутку"],
14
- title="Echo Bot на Gradio"
15
  )
16
 
17
  if __name__ == "__main__":
18
- demo.launch()
 
1
  # app.py
2
  import gradio as gr
3
+ from transformers import pipeline
4
 
5
+ # Инициализируем модель text-generation (например, LLaMA / smolLM)
6
+ chat_model = pipeline(
7
+ "text-generation",
8
+ model="HuggingFaceTB/SmolLM2-135M-Instruct",
9
+ device_map="auto", # вариант: "cpu" если без GPU
10
+ )
11
+
12
+ def chat_fn(message, history):
13
+ """
14
+ message: str — запрос пользователя
15
+ history: list of dict {'role':..., 'content':...}
16
+ """
17
  history = history or []
18
+ # Добавляем сообщение пользователя в историю
19
+ history.append({"role": "user", "content": message})
20
+ # Формируем вход для модели
21
+ full_prompt = "\n".join(f"{m['role']}: {m['content']}" for m in history)
22
+ output = chat_model(full_prompt, max_new_tokens=100, do_sample=True)
23
+ reply = output[0]["generated_text"].split(full_prompt)[-1].strip()
24
+ # Добавляем ответ в историю
25
+ history.append({"role": "assistant", "content": reply})
26
+ return reply, history
27
 
28
+ iface = gr.ChatInterface(
29
+ fn=chat_fn,
30
  type="messages",
31
+ title="Gradio + transformers Chat",
32
+ examples=["Привет!", "Расскажи анекдот", "Что такое LLaMA?"],
33
  )
34
 
35
  if __name__ == "__main__":
36
+ iface.launch()