Illia56 commited on
Commit
88d78fa
·
1 Parent(s): b7e8e8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -87
app.py CHANGED
@@ -1,25 +1,17 @@
 
1
  import gradio as gr
2
  from gradio_client import Client
3
 
4
-
5
-
6
- system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
7
- temperature = 0.9
8
- top_p = 0.6
9
- repetition_penalty = 1.2
10
-
11
  title = "Llama2 70B Chatbot"
 
12
  description = """
13
  This Space demonstrates model [Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) by Meta, a Llama 2 model with 70B parameters fine-tuned for chat instructions.
14
- It relies on 2 models:
15
- 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).
16
- 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).
17
  | Model | Llama2 | Llama2-hf | Llama2-chat | Llama2-chat-hf |
18
  |---|---|---|---|---|
19
  | 70B | [Link](https://huggingface.co/meta-llama/Llama-2-70b) | [Link](https://huggingface.co/meta-llama/Llama-2-70b-hf) | [Link](https://huggingface.co/meta-llama/Llama-2-70b-chat) | [Link](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) |
20
-
21
  """
22
  css = """.toast-wrap { display: none !important } """
 
23
  examples=[
24
  ['Hello there! How are you doing?'],
25
  ['Can you explain to me briefly what is Python programming language?'],
@@ -28,96 +20,39 @@ examples=[
28
  ["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
29
  ]
30
 
31
- whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
32
- text_client = Client("https://ysharma-explore-llamav2-with-tgi.hf.space/")
33
 
34
 
35
  def transcribe(wav_path):
 
36
 
37
  return whisper_client.predict(
38
  wav_path, # str (filepath or URL to file) in 'inputs' Audio component
39
  "transcribe", # str in 'Task' Radio component
40
  api_name="/predict"
41
- )
42
-
43
-
44
- # Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
45
-
46
-
47
- def add_text(history, text):
48
- history = [] if history is None else history
49
- history = history + [(text, None)]
50
- return history, gr.update(value="", interactive=False)
51
-
52
-
53
- def add_file(history, file):
54
- history = [] if history is None else history
55
- text = transcribe(
56
- file
57
  )
58
-
59
- history = history + [(text, None)]
60
- return history
61
-
62
 
63
 
64
- def bot(history, system_prompt=""):
65
- history = [] if history is None else history
66
-
67
- if system_prompt == "":
68
- system_prompt = system_message
69
-
70
- history[-1][1] = ""
71
- for character in text_client.submit(
72
- history,
73
- system_prompt,
74
- temperature,
75
- 4096,
76
- temperature,
77
- repetition_penalty,
78
- api_name="/chat_1"
79
- ):
80
- history[-1][1] = character
81
- yield history
82
-
83
 
84
 
85
 
86
- with gr.Blocks(title=title,theme=gr.themes.Base(),description = description) as demo:
87
-
88
-
89
- chatbot = gr.Chatbot(
90
- [],
91
- elem_id="chatbot",
92
- bubble_full_width=False,
93
- )
94
 
95
- with gr.Row():
96
- txt = gr.Textbox(
97
- scale=3,
98
- show_label=False,
99
- placeholder="Enter text and press enter, or speak to your microphone",
100
- container=False,
101
- )
102
- txt_btn = gr.Button(value="Submit text",scale=1)
103
- btn = gr.Audio(source="microphone", type="filepath", scale=4)
104
-
105
- # with gr.Row():
106
- # Ex = gr.Examples(examples, [txt_btn], [chatbot],fn =add_text )
107
-
108
- clear_btn = gr.ClearButton([chatbot])
109
-
110
- txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then(
111
- bot, chatbot, chatbot
112
- )
113
-
114
- txt_msg.then(lambda: gr.update(interactive=True), None, [txt])
115
-
116
- file_msg = btn.stop_recording(add_file, [chatbot, btn], [chatbot]).then(
117
- bot, chatbot, chatbot
118
- )
119
-
120
 
 
 
 
 
121
 
122
- demo.queue()
123
- demo.launch(debug=True)
 
1
+
2
  import gradio as gr
3
  from gradio_client import Client
4
 
 
 
 
 
 
 
 
5
  title = "Llama2 70B Chatbot"
6
+
7
  description = """
8
  This Space demonstrates model [Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) by Meta, a Llama 2 model with 70B parameters fine-tuned for chat instructions.
 
 
 
9
  | Model | Llama2 | Llama2-hf | Llama2-chat | Llama2-chat-hf |
10
  |---|---|---|---|---|
11
  | 70B | [Link](https://huggingface.co/meta-llama/Llama-2-70b) | [Link](https://huggingface.co/meta-llama/Llama-2-70b-hf) | [Link](https://huggingface.co/meta-llama/Llama-2-70b-chat) | [Link](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) |
 
12
  """
13
  css = """.toast-wrap { display: none !important } """
14
+
15
  examples=[
16
  ['Hello there! How are you doing?'],
17
  ['Can you explain to me briefly what is Python programming language?'],
 
20
  ["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
21
  ]
22
 
23
+
 
24
 
25
 
26
  def transcribe(wav_path):
27
+ whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
28
 
29
  return whisper_client.predict(
30
  wav_path, # str (filepath or URL to file) in 'inputs' Audio component
31
  "transcribe", # str in 'Task' Radio component
32
  api_name="/predict"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  )
 
 
 
 
34
 
35
 
36
+ # Stream text
37
+ def predict(message, chatbot, system_prompt="", temperature=0.9, max_new_tokens=4096):
38
+
39
+ client = Client("https://ysharma-explore-llamav2-with-tgi.hf.space/")
40
+ return client.predict(transcribe(message), # str in 'Message' Textbox component
41
+ system_prompt, # str in 'Optional system prompt' Textbox component
42
+ temperature, # int | float (numeric value between 0.0 and 1.0)
43
+ max_new_tokens, # int | float (numeric value between 0 and 4096)
44
+ 0.3, # int | float (numeric value between 0.0 and 1)
45
+ 1, # int | float (numeric value between 1.0 and 2.0)
46
+ api_name="/chat_1")
 
 
 
 
 
 
 
 
47
 
48
 
49
 
 
 
 
 
 
 
 
 
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ # Gradio Demo
53
+ with gr.Blocks(theme=gr.themes.Base()) as demo:
54
+ gr.DuplicateButton()
55
+ gr.ChatInterface(predict, title=title, inputs="audio", description=description, css=css, examples=examples)
56
 
57
+
58
+ demo.queue().launch(debug=True)