ldhldh commited on
Commit
46e3fab
1 Parent(s): 698f4e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -118
app.py CHANGED
@@ -1,121 +1,88 @@
 
1
  import gradio as gr
2
- from gradio import routes
3
- from typing import List, Type
4
-
5
- import requests, os, re, asyncio, queue
6
- import math
7
  import time
8
- import datetime
9
- import requests, json
10
-
11
- loop = asyncio.get_event_loop()
12
- # Monkey patch
13
- def get_types(cls_set: List[Type], component: str):
14
- docset = []
15
- types = []
16
- if component == "input":
17
- for cls in cls_set:
18
- doc = inspect.getdoc(cls)
19
- doc_lines = doc.split("\n")
20
- docset.append(doc_lines[1].split(":")[-1])
21
- types.append(doc_lines[1].split(")")[0].split("(")[-1])
22
- else:
23
- for cls in cls_set:
24
- doc = inspect.getdoc(cls)
25
- doc_lines = doc.split("\n")
26
- docset.append(doc_lines[-1].split(":")[-1])
27
- types.append(doc_lines[-1].split(")")[0].split("(")[-1])
28
- return docset, types
29
- routes.get_types = get_types
30
-
31
- user_data = dict()
32
- live_user = dict()
33
- chat_history = []
34
-
35
-
36
- def register(id, pw):
37
- if not id in user_data:
38
- user_data[id] = pw
39
- return "ok"
40
- else:
41
- return "fail"
42
-
43
- def login(id, pw):
44
- if not id in user_data:
45
- return "fail"
46
- else:
47
- if user_data[id] != pw:
48
- return "fail"
49
- else:
50
- live_user[id] = 20
51
- return "ok"
52
-
53
- def chat(name, text, time):
54
- if not name in user_data:
55
- return "no id"
56
- else:
57
- chat_history.append({"name": name, "text":text, "time":time})
58
- return "ok"
59
-
60
- def get_data(name):
61
- global live_user
62
- for u in live_user.keys():
63
- if u == name:
64
- live_user[u] = 20
65
- else:
66
- live_user[u] -= 1
67
- if live_user[u] < 0:
68
- del live_user[u]
69
- return chat_history
70
-
71
- def get_live_user():
72
- return live_user.keys()
73
-
74
- def clear_data():
75
- global chat_history
76
- chat_history = []
77
- return "ok"
78
-
79
-
80
- with gr.Blocks() as demo:
81
- count = 0
82
- gr.Markdown(
83
- f"{chat_history}"
84
- )
85
- aa = gr.Interface(
86
- fn=chat,
87
- inputs=["text", "text", "text"],
88
- outputs="text",
89
- description="chat",
90
- )
91
- bb = gr.Interface(
92
- fn=login,
93
- inputs=["text", "text"],
94
- outputs="text",
95
- description="login",
96
- )
97
- cc = gr.Interface(
98
- fn=register,
99
- inputs=["text", "text"],
100
- outputs="text",
101
- description="register",
102
- )
103
- dd = gr.Interface(
104
- fn=get_data,
105
- inputs=["text"],
106
- outputs="text",
107
- description="get_data",
108
- )
109
- gg = gr.Interface(
110
- fn=get_live_user,
111
- inputs=[],
112
- outputs="text",
113
- description="get_live_user",
114
- )
115
- ss = gr.Interface(
116
- fn=clear_data,
117
- inputs=[],
118
- outputs="text",
119
- description="clear_data",
120
  )
121
- demo.queue(max_size=32).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
3
+ import whisper
4
+ from whisper import tokenizer
 
 
 
5
  import time
6
+
7
+ current_size = 'base'
8
+ model = whisper.load_model(current_size)
9
+ AUTO_DETECT_LANG = "Auto Detect"
10
+
11
+ def transcribe(audio, state={}, model_size='base', delay=1.2, lang=None, translate=False):
12
+ time.sleep(delay - 1)
13
+
14
+ global current_size
15
+ global model
16
+ if model_size != current_size:
17
+ current_size = model_size
18
+ model = whisper.load_model(current_size)
19
+
20
+ transcription = model.transcribe(
21
+ audio,
22
+ language = lang if lang != AUTO_DETECT_LANG else None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  )
24
+ state['transcription'] += transcription['text'] + " "
25
+
26
+ if translate:
27
+ x = whisper.load_audio(audio)
28
+ x = whisper.pad_or_trim(x)
29
+ mel = whisper.log_mel_spectrogram(x).to(model.device)
30
+
31
+ options = whisper.DecodingOptions(task = "translation")
32
+ translation = whisper.decode(model, mel, options)
33
+
34
+ state['translation'] += translation.text + " "
35
+
36
+ return state['transcription'], state['translation'], state, f"detected language: {transcription['language']}"
37
+
38
+
39
+ title = "OpenAI's Whisper Real-time Demo"
40
+ description = "A simple demo of OpenAI's [**Whisper**](https://github.com/openai/whisper) speech recognition model. This demo runs on a CPU. For faster inference choose 'tiny' model size and set the language explicitly."
41
+
42
+ model_size = gr.Dropdown(label="Model size", choices=['base', 'tiny', 'small', 'medium', 'large'], value='base')
43
+
44
+ delay_slider = gr.inputs.Slider(minimum=1, maximum=5, default=1.2, label="Rate of transcription")
45
+
46
+ available_languages = sorted(tokenizer.TO_LANGUAGE_CODE.keys())
47
+ available_languages = [lang.capitalize() for lang in available_languages]
48
+ available_languages = [AUTO_DETECT_LANG]+available_languages
49
+
50
+ lang_dropdown = gr.inputs.Dropdown(choices=available_languages, label="Language", default=AUTO_DETECT_LANG, type="value")
51
+
52
+ if lang_dropdown==AUTO_DETECT_LANG:
53
+ lang_dropdown=None
54
+
55
+ translate_checkbox = gr.inputs.Checkbox(label="Translate to English", default=False)
56
+
57
+
58
+
59
+ transcription_tb = gr.Textbox(label="Transcription", lines=10, max_lines=20)
60
+ translation_tb = gr.Textbox(label="Translation", lines=10, max_lines=20)
61
+ detected_lang = gr.outputs.HTML(label="Detected Language")
62
+
63
+ state = gr.State({"transcription": "", "translation": ""})
64
+
65
+ gr.Interface(
66
+ fn=transcribe,
67
+ inputs=[
68
+ gr.Audio(source="microphone", type="filepath", streaming=True),
69
+ state,
70
+ model_size,
71
+ delay_slider,
72
+ lang_dropdown,
73
+ translate_checkbox
74
+ ],
75
+ outputs=[
76
+ transcription_tb,
77
+ translation_tb,
78
+ state,
79
+ detected_lang
80
+ ],
81
+ live=True,
82
+ allow_flagging='never',
83
+ title=title,
84
+ description=description,
85
+ ).launch(
86
+ # enable_queue=True,
87
+ # debug=True
88
+ )