Spaces:
Running
Running
Update app.py
Browse files
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 with chat and token streaming functionality.
|
4 |
|
5 |
import gradio as gr
|
6 |
from transformers import pipeline
|
@@ -10,17 +10,21 @@ model_name = "MTSAIR/Cotype-Nano"
|
|
10 |
pipe = pipeline("text-generation", model=model_name, device="cpu")
|
11 |
|
12 |
# Define the system prompt
|
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=
|
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")
|
@@ -30,10 +34,21 @@ with gr.Blocks() as demo:
|
|
30 |
with gr.Row():
|
31 |
txt = gr.Textbox(
|
32 |
show_label=False,
|
33 |
-
placeholder="
|
34 |
)
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Launch the interface
|
39 |
if __name__ == "__main__":
|
|
|
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, advanced settings, and English interface.
|
4 |
|
5 |
import gradio as gr
|
6 |
from transformers import pipeline
|
|
|
10 |
pipe = pipeline("text-generation", model=model_name, device="cpu")
|
11 |
|
12 |
# Define the system prompt
|
13 |
+
system_prompt = {"role": "system", "content": "You are an AI assistant. Your task is to generate a detailed and comprehensive response."}
|
14 |
|
15 |
# Define the Gradio interface
|
16 |
+
def generate_response(history, user_input, temperature, max_tokens):
|
17 |
messages = [system_prompt] + history + [{"role": "user", "content": user_input}]
|
18 |
+
response = pipe(messages, max_length=max_tokens, temperature=temperature, 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 |
+
# Function to clear chat history
|
25 |
+
def clear_chat():
|
26 |
+
return [], ""
|
27 |
+
|
28 |
# Create the Gradio interface
|
29 |
with gr.Blocks() as demo:
|
30 |
gr.Markdown("## Cotype-Nano Text Generation Chat")
|
|
|
34 |
with gr.Row():
|
35 |
txt = gr.Textbox(
|
36 |
show_label=False,
|
37 |
+
placeholder="Type your message here...",
|
38 |
)
|
39 |
|
40 |
+
send_btn = gr.Button("Send")
|
41 |
+
|
42 |
+
with gr.Row():
|
43 |
+
clear_btn = gr.Button("Clear Chat")
|
44 |
+
|
45 |
+
with gr.Row():
|
46 |
+
temperature_slider = gr.Slider(0, 1, 0.7, step=0.1, label="Temperature")
|
47 |
+
max_tokens_slider = gr.Slider(1, 1000, 100, step=1, label="Max Tokens")
|
48 |
+
|
49 |
+
send_btn.click(generate_response, [chatbot, txt, temperature_slider, max_tokens_slider], [chatbot, txt])
|
50 |
+
txt.submit(generate_response, [chatbot, txt, temperature_slider, max_tokens_slider], [chatbot, txt])
|
51 |
+
clear_btn.click(clear_chat, outputs=[chatbot, txt])
|
52 |
|
53 |
# Launch the interface
|
54 |
if __name__ == "__main__":
|