Ivan000 commited on
Commit
87947e6
·
verified ·
1 Parent(s): 71282ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -16
app.py CHANGED
@@ -1,6 +1,6 @@
1
  # app.py
2
  # =============
3
- # This is a complete app.py file for deploying the MTSAIR/Cotype-Nano model using Gradio and Hugging Face Transformers.
4
 
5
  import gradio as gr
6
  from transformers import pipeline
@@ -13,23 +13,28 @@ pipe = pipeline("text-generation", model=model_name, device="cpu")
13
  system_prompt = {"role": "system", "content": "Ты — ИИ-помощник. Тебе дано задание: необходимо сгенерировать подробный и развернутый ответ."}
14
 
15
  # Define the Gradio interface
16
- def generate_response(user_input):
17
- messages = [
18
- system_prompt,
19
- {"role": "user", "content": user_input}
20
- ]
21
- response = pipe(messages, max_length=1024)
22
- return response[0]['generated_text']
23
 
24
  # Create the Gradio interface
25
- iface = gr.Interface(
26
- fn=generate_response,
27
- inputs=gr.Textbox(lines=2, placeholder="Введите ваш запрос здесь..."),
28
- outputs="text",
29
- title="Cotype-Nano Text Generation",
30
- description="Введите ваш запрос, и Cotype-Nano сгенерирует ответ."
31
- )
 
 
 
 
 
32
 
33
  # Launch the interface
34
  if __name__ == "__main__":
35
- iface.launch()
 
1
  # app.py
2
  # =============
3
+ # This is a complete app.py file for deploying the MTSAIR/Cotype-Nano model using Gradio and Hugging Face Transformers with chat and token streaming functionality.
4
 
5
  import gradio as gr
6
  from transformers import pipeline
 
13
  system_prompt = {"role": "system", "content": "Ты — ИИ-помощник. Тебе дано задание: необходимо сгенерировать подробный и развернутый ответ."}
14
 
15
  # Define the Gradio interface
16
+ def generate_response(history, user_input):
17
+ messages = [system_prompt] + history + [{"role": "user", "content": user_input}]
18
+ response = pipe(messages, max_length=1024, return_full_text=False)
19
+ generated_text = response[0]['generated_text']
20
+ history.append({"role": "user", "content": user_input})
21
+ history.append({"role": "assistant", "content": generated_text})
22
+ return history, ""
23
 
24
  # Create the Gradio interface
25
+ with gr.Blocks() as demo:
26
+ gr.Markdown("## Cotype-Nano Text Generation Chat")
27
+
28
+ chatbot = gr.Chatbot([], elem_id="chatbot")
29
+
30
+ with gr.Row():
31
+ txt = gr.Textbox(
32
+ show_label=False,
33
+ placeholder="Введите ваш запрос здесь...",
34
+ ).style(container=False)
35
+
36
+ txt.submit(generate_response, [chatbot, txt], [chatbot, txt])
37
 
38
  # Launch the interface
39
  if __name__ == "__main__":
40
+ demo.launch()