Illia56 commited on
Commit
c522abc
1 Parent(s): b122401

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -17
app.py CHANGED
@@ -21,28 +21,107 @@ examples=[
21
  ["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
22
  ]
23
 
 
 
24
 
25
- # Stream text
26
- def predict(message, chatbot, system_prompt="", temperature=0.9, max_new_tokens=4096):
27
-
28
- client = Client("https://ysharma-explore-llamav2-with-tgi.hf.space/")
29
- return client.predict(
30
- message, # str in 'Message' Textbox component
31
- system_prompt, # str in 'Optional system prompt' Textbox component
32
- temperature, # int | float (numeric value between 0.0 and 1.0)
33
- max_new_tokens, # int | float (numeric value between 0 and 4096)
34
- 0.3, # int | float (numeric value between 0.0 and 1)
35
- 1, # int | float (numeric value between 1.0 and 2.0)
36
- api_name="/chat"
 
 
 
 
 
 
 
 
 
 
 
37
  )
 
 
 
 
 
 
 
 
 
 
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
40
 
41
 
 
 
 
 
 
 
 
 
 
42
 
43
- # Gradio Demo
44
- with gr.Blocks(theme=gr.themes.Base()) as demo:
45
- gr.DuplicateButton()
46
- gr.ChatInterface(predict, title=title, description=description, css=css, examples=examples)
 
 
 
 
 
 
47
 
48
- demo.queue().launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  ["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
22
  ]
23
 
24
+ whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
25
+ text_client = Client("https://ysharma-explore-llamav2-with-tgi.hf.space/")
26
 
27
+
28
+ def transcribe(wav_path):
29
+
30
+ return whisper_client.predict(
31
+ wav_path, # str (filepath or URL to file) in 'inputs' Audio component
32
+ "transcribe", # str in 'Task' Radio component
33
+ api_name="/predict"
34
+ )
35
+
36
+
37
+ # Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
38
+
39
+
40
+ def add_text(history, text):
41
+ history = [] if history is None else history
42
+ history = history + [(text, None)]
43
+ return history, gr.update(value="", interactive=False)
44
+
45
+
46
+ def add_file(history, file):
47
+ history = [] if history is None else history
48
+ text = transcribe(
49
+ file
50
  )
51
+
52
+ history = history + [(text, None)]
53
+ return history
54
+
55
+
56
+
57
+ def bot(history, system_prompt=""):
58
+ history = [] if history is None else history
59
+
60
+ if system_prompt == "":
61
+ system_prompt = system_message
62
 
63
+ history[-1][1] = ""
64
+ for character in text_client.submit(
65
+ history,
66
+ system_prompt,
67
+ temperature,
68
+ 4096,
69
+ temperature,
70
+ repetition_penalty,
71
+ api_name="/chat"
72
+ ):
73
+ history[-1][1] = character
74
+ yield history
75
 
76
+
77
 
78
 
79
+ with gr.Blocks(title=title) as demo:
80
+ gr.Markdown(DESCRIPTION)
81
+
82
+
83
+ chatbot = gr.Chatbot(
84
+ [],
85
+ elem_id="chatbot",
86
+ bubble_full_width=False,
87
+ )
88
 
89
+ with gr.Row():
90
+ txt = gr.Textbox(
91
+ scale=3,
92
+ show_label=False,
93
+ placeholder="Enter text and press enter, or speak to your microphone",
94
+ container=False,
95
+ )
96
+ txt_btn = gr.Button(value="Submit text",scale=1)
97
+ btn = gr.Audio(source="microphone", type="filepath", scale=4)
98
+ gradio.Examples(examples, txt_btn)
99
 
100
+ with gr.Row():
101
+ audio = gr.Audio(type="numpy", streaming=True, autoplay=True, label="Generated audio response", show_label=True)
102
+
103
+ clear_btn = gr.ClearButton([chatbot, audio])
104
+
105
+ txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
106
+ bot, chatbot, chatbot
107
+ ).
108
+
109
+ txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
110
+ bot, chatbot, chatbot
111
+ ).
112
+
113
+ txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
114
+
115
+ file_msg = btn.stop_recording(add_file, [chatbot, btn], [chatbot], queue=False).then(
116
+ bot, chatbot, chatbot
117
+ ).
118
+
119
+
120
+ gr.Markdown("""
121
+ This Space demonstrates how to speak to a chatbot, based solely on open-source models.
122
+ It relies on 3 models:
123
+ 1. [Whisper-large-v2](https://huggingface.co/spaces/sanchit-gandhi/whisper-large-v2) as an ASR model, to transcribe recorded audio to text. It is called through a [gradio client](https://www.gradio.app/docs/client).
124
+ 2. [Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) as the chat model, the actual chat model. It is also called through a [gradio client](https://www.gradio.app/docs/client).
125
+ """)
126
+ demo.queue()
127
+ demo.launch(debug=True)