nandovallec commited on
Commit
da9f906
·
1 Parent(s): d3dcf1b
Files changed (1) hide show
  1. app.py +133 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from transformers import pipeline
2
+ # import gradio as gr
3
+ # AIzaSyDeT8V0nRlVEgmb0fMK4uc0ci8fAcS0Olg
4
+ # pipe = pipeline(model="sanchit-gandhi/whisper-small-hi") # change to "your-username/the-name-you-picked"
5
+ import requests
6
+ import json
7
+ import gradio as gr
8
+ from transformers import pipeline
9
+ import re
10
+ import asyncio
11
+ pipe = pipeline(model="nandovallec/whisper-tiny-bg-l") # change to "your-username/the-name-you-picked"
12
+ gmaps_api_key = GMAPS_API
13
+ transl_api_key= TRANS_API
14
+ # gmaps.configure(api_key=gmaps_api_key)
15
+ # mode = "walking"
16
+
17
+
18
+
19
+ def translate(text):
20
+ # text = "Hello world"
21
+ source = "bg"
22
+ target = "en"
23
+
24
+ # Construct the URL for the request
25
+ url = f"https://translation.googleapis.com/language/translate/v2?key={transl_api_key}&q={text}&source={source}&target={target}"
26
+
27
+ response = requests.get(url)
28
+
29
+ # Check the status code to make sure the request was successful
30
+ if response.status_code == 200:
31
+ # If the request was successful, print the translated text
32
+ response_json = json.loads(response.text)
33
+ return (response_json["data"]["translations"][0]["translatedText"])
34
+ else:
35
+ # If the request was not successful, print the error message
36
+ return (f"Request failed: {response.text}")
37
+
38
+ def route_written(text):
39
+ translation = translate(text)
40
+ print(f"Translated: {translation}")
41
+ results = re.search('from (.*)to (.*)', translation)
42
+ print(results.groups())
43
+ origin = results.group(1)
44
+ # print(origin)
45
+ dest = results.group(2)
46
+ # print(dest)
47
+
48
+ return create_iframe(origin, dest, "walking")
49
+
50
+ def transcribe(audio, mode):
51
+ text = pipe(audio)["text"]
52
+ print(f"Original text: {text}")
53
+ translation = translate(text)
54
+ print(f"Translated: {translation}")
55
+ results = re.search('from (.*)to (.*)', translation)
56
+ print(results.groups())
57
+ origin = results.group(1)
58
+ # print(origin)
59
+ dest = results.group(2)
60
+ # print(dest)
61
+
62
+ return create_iframe(origin, dest, mode)
63
+
64
+ def create_iframe(origin, destination,mode):
65
+ # global mode
66
+ # Create an iframe with the name as the title
67
+ # iframe = "<iframe src=\"demo_iframe.html\" height=\"200\" width=\"300\" title=\"Iframe Example\"></iframe>"
68
+ # frame=f"<iframe id=\"inlineFrameExample\"title=\"Inline Frame Map\" \"height=\"auto\" style=\"width:100%\" src=\"https://www.google.com/maps/embed/v1/directions?key={gmaps_api_key}&origin={origin}&destination={destination}&mode={mode}\"></iframe>"
69
+ # frame = f"https://www.google.com/maps/embed/v1/directions?key={gmaps_api_key}&origin={origin}&destination={destination}&mode={mode}"
70
+ frame=f'<iframe id="inlineFrameExample" title="Inline Frame Map" style="width:100%; height: 500px;" src="https://www.google.com/maps/embed/v1/directions?key={gmaps_api_key}&origin={origin}&destination={destination}&mode={mode}"></iframe>'
71
+
72
+ return frame
73
+
74
+
75
+ def route_walking(audio):
76
+ return transcribe(audio, "walking")
77
+
78
+
79
+ def route_transit(audio):
80
+ return transcribe(audio, "transit")
81
+
82
+
83
+ def route_driving(audio):
84
+ return transcribe(audio, "driving")
85
+
86
+
87
+ with gr.Blocks() as app1:
88
+ # global mode
89
+
90
+ btn = gr.Button(value="Submit")
91
+ ifr = gr.HTML()
92
+ btn.click(route_walking, inputs=[gr.Audio(source="microphone", type="filepath")], outputs=[ifr])
93
+
94
+
95
+
96
+ # app1 = gr.Interface(
97
+ # fn = create_iframe,
98
+ # inputs=gr.inputs.Textbox(label="Enter a place:"),
99
+ # outputs="html"
100
+ # ).launch()
101
+
102
+ with gr.Blocks() as app2:
103
+ # global mode
104
+
105
+ btn = gr.Button(value="Submit")
106
+ ifr = gr.HTML()
107
+ btn.click(route_transit, inputs=[gr.Audio(source="microphone", type="filepath")], outputs=[ifr])
108
+
109
+ with gr.Blocks() as app3:
110
+ # txt = gr.Textbox(label="some text")
111
+ btn = gr.Button(value="Submit")
112
+ ifr = gr.HTML()
113
+ btn.click(route_driving, inputs=[gr.Audio(source="microphone", type="filepath")], outputs=[ifr])
114
+
115
+ with gr.Blocks() as app4:
116
+ # global mode
117
+
118
+ txt = gr.Textbox(label="Written text")
119
+ btn = gr.Button(value="Submit")
120
+
121
+ ifr = gr.HTML()
122
+
123
+ btn.click(route_written, inputs=[txt], outputs=[ifr])
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+ demo = gr.TabbedInterface([app1, app2, app3, app4], ["Walking", "Public Transport","Car", "Written Walking"])
133
+ demo.launch()