File size: 4,258 Bytes
da9f906
 
 
 
 
 
 
 
 
 
7d0eeb6
8a210bf
da9f906
0188bfa
 
da9f906
 
 
 
 
 
 
 
 
2d25fd0
 
da9f906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d25fd0
da9f906
2d25fd0
da9f906
 
 
 
 
 
 
 
 
2d25fd0
da9f906
2d25fd0
da9f906
2d25fd0
da9f906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# from transformers import pipeline
# import gradio as gr
# AIzaSyDeT8V0nRlVEgmb0fMK4uc0ci8fAcS0Olg
# pipe = pipeline(model="sanchit-gandhi/whisper-small-hi")  # change to "your-username/the-name-you-picked"
import requests
import json
import gradio as gr
from transformers import pipeline
import re
import asyncio
import os

pipe = pipeline(model="nandovallec/whisper-tiny-bg-l")  # change to "your-username/the-name-you-picked"
gmaps_api_key = os.environ["GMAPS_API"]
transl_api_key= os.environ["TRANS_API"]
# gmaps.configure(api_key=gmaps_api_key)
# mode = "walking"



def translate(text):
# text = "Hello world"
    source = "bg"
    target = "en"
   # print(gmaps_api_key)
   # print(transl_api_key)
    # Construct the URL for the request
    url = f"https://translation.googleapis.com/language/translate/v2?key={transl_api_key}&q={text}&source={source}&target={target}"

    response = requests.get(url)

    # Check the status code to make sure the request was successful
    if response.status_code == 200:
      # If the request was successful, print the translated text
      response_json = json.loads(response.text)
      return (response_json["data"]["translations"][0]["translatedText"])
    else:
      # If the request was not successful, print the error message
      return (f"Request failed: {response.text}")

def route_written(text):
    translation = translate(text)
#    print(f"Translated: {translation}")
    results = re.search('from (.*)to (.*)', translation)
#    print(results.groups())
    origin = results.group(1)
    # print(origin)
    dest = results.group(2)
    # print(dest)

    return create_iframe(origin, dest, "walking")

def transcribe(audio, mode):
    text = pipe(audio)["text"]
 #   print(f"Original text: {text}")
    translation = translate(text)
 #   print(f"Translated: {translation}")
    results = re.search('from (.*)to (.*)', translation)
  #  print(results.groups())
    origin = results.group(1)
    # print(origin)
    dest = results.group(2)
    # print(dest)

    return create_iframe(origin, dest, mode)

def create_iframe(origin, destination,mode):
    # global mode
    # Create an iframe with the name as the title
    # iframe = "<iframe src=\"demo_iframe.html\" height=\"200\" width=\"300\" title=\"Iframe Example\"></iframe>"
    # 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>"
    # frame = f"https://www.google.com/maps/embed/v1/directions?key={gmaps_api_key}&origin={origin}&destination={destination}&mode={mode}"
    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>'

    return frame


def route_walking(audio):
    return transcribe(audio, "walking")


def route_transit(audio):
    return transcribe(audio, "transit")


def route_driving(audio):
    return transcribe(audio, "driving")


with gr.Blocks() as app1:
    # global mode

    btn = gr.Button(value="Submit")
    ifr = gr.HTML()
    btn.click(route_walking, inputs=[gr.Audio(source="microphone", type="filepath")], outputs=[ifr])



# app1 = gr.Interface(
#   fn = create_iframe,
#   inputs=gr.inputs.Textbox(label="Enter a place:"),
#   outputs="html"
# ).launch()

with gr.Blocks() as app2:
    # global mode

    btn = gr.Button(value="Submit")
    ifr = gr.HTML()
    btn.click(route_transit, inputs=[gr.Audio(source="microphone", type="filepath")], outputs=[ifr])

with gr.Blocks() as app3:
    # txt = gr.Textbox(label="some text")
    btn = gr.Button(value="Submit")
    ifr = gr.HTML()
    btn.click(route_driving, inputs=[gr.Audio(source="microphone", type="filepath")], outputs=[ifr])

with gr.Blocks() as app4:
    # global mode

    txt = gr.Textbox(label="Written text")
    btn = gr.Button(value="Submit")

    ifr = gr.HTML()

    btn.click(route_written, inputs=[txt], outputs=[ifr])








demo = gr.TabbedInterface([app1, app2, app3, app4], ["Walking", "Public Transport","Car", "Written Walking"])
demo.launch()