# 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 = ""
# frame=f""
# frame = f"https://www.google.com/maps/embed/v1/directions?key={gmaps_api_key}&origin={origin}&destination={destination}&mode={mode}"
frame=f''
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()