tomriddle/cuda
0
1import pathlib2import uuid3import os4import gradio as gr5from tqdm import tqdm6import requests7import urllib.request8import json9import time10output_mp3="output.mp3"11 12def upload_image(img: str, d_id_key: str):13 url = "https://api.d-id.com/images"14 15 files = {"image": ("hero.jpg", open(img, "rb"), "image/jpg")}16 headers = {17 "accept": "application/json",18 "authorization": "Basic "+d_id_key19 }20 21 response = requests.post(url, files=files, headers=headers)22 23 response_dict = response.json()24 img_url = response_dict["url"]25 # return audio_url26 print(img_url)27 return img_url28 29def upload_audio(audio: str, d_id_key: str):30 url = "https://api.d-id.com/audios"31 32 files = {"audio": (audio, open(audio, "rb"), "audio/mpeg")}33 headers = {34 "accept": "application/json",35 "authorization": "Basic "+d_id_key36 }37 response = requests.post(url, files=files, headers=headers)38 response_dict = response.json()39 audio_url = response_dict["url"]40 # return audio_url41 print(audio_url)42 return audio_url43 44def get_did_video(process_video_url,d_id_key):45 url = "https://api.d-id.com/talks/"+process_video_url46 47 headers = {48 "accept": "application/json",49 "authorization": "Basic "+d_id_key50 }51 response_dict = {}52 53 while "result_url" not in response_dict:54 # make API call and get response dictionary55 response = requests.get(url, headers=headers)56 response_dict = response.json()57 58 print(response.text)59 60 # wait for 1 second before checking again61 time.sleep(1)62 63 # "result_url" key is now present in the dictionary64 result_url = response_dict["result_url"]65 66 print("From did_video \n\n\n")67 print("/n/n/n")68 69 # response_dict = response.json()70 result_url = response_dict["result_url"]71 72 print(result_url)73 return result_url74 75def text_to_speach_api(text: str, elv_key,voice_id: str):76 url = "https://api.elevenlabs.io/v1/text-to-speech/"+voice_id+"/stream"77 headers = {78 "accept": "*/*",79 "xi-api-key": elv_key,80 "Content-Type": "application/json",81 }82 data = {83 "text": text,84 "voice_settings": {85 "stability": 0,86 "similarity_boost": 087 }88 }89 90 response = requests.post(url, headers=headers, json=data)91 # print(response.text)92 93 if response.ok:94 with open("output.mp3", "wb") as f:95 f.write(response.content)96 else:97 print("Error: ", response.text)98 99def get_voice_names():100 with open("data.json") as f:101 data = json.load(f)102 return [voice["name"] for voice in data["voices"]]103 104 105 106# define a function to get voice id by name107def get_voice_id(name):108 # load the JSON data109 with open("data.json") as f:110 data = json.load(f)111 for voice in data['voices']:112 if voice['name'] == name:113 return voice['voice_id']114 return None115 116#D-id API117def d_id_api(image_url, d_id_key,audio_url):118 print("D-id API")119 url = "https://api.d-id.com/talks"120 payload = {121 122 "source_url": image_url,123 "script": {124 "type": "audio",125 "audio_url": audio_url,126 }127 128 }129 headers = {130 "accept": "application/json",131 "content-type": "application/json",132 "authorization": "Basic "+d_id_key133 }134 135 response = requests.post(url, json=payload, headers=headers)136 print("From D-id API \n\n\n")137 print(response.text)138 response_dict = response.json()139 process_video = response_dict["id"]140 # return audio_url141 print(process_video)142 return process_video143 144 145 146def transcribe_video(d_id_key: str, elv_key: str, full_text: str,voice_name: str,img):147 print(voice_name)148 voice_id=get_voice_id(voice_name)149 text_to_speach_api(full_text, elv_key,voice_id)150 audio_url=upload_audio(output_mp3,d_id_key)151 image_url=upload_image(img,d_id_key)152 process_video_url=d_id_api(image_url, d_id_key,audio_url)153 video_url=get_did_video(process_video_url,d_id_key)154 file_name = 'hero.mp4'155 urllib.request.urlretrieve(video_url, file_name)156 return file_name157 158 159examples = [["", "","Good morning, it's great to see you! I hope you're having a wonderful day. I just wanted to say thank you for taking the time to speak with me. Is there anything new or exciting happening in your life? I'd love to hear about it. Let's catch up soon!",160 "Arnold","./images/hero.jpg"],["","","Hello there, I'm a talking photo! I can speak any text you type here. Try it out!", "Domi","./images/3.jpg"],["","","Hello there, I'm a talking photo! I can speak any text you type here. Try it out!", "Domi","./images/2.jpg"]]161 162demo = gr.Interface(fn=transcribe_video, inputs=[163 gr.Textbox(label="D-Id API Key",placeholder="Paste your D-Id",type='password'),164 gr.Textbox(label="Elevenlabs API Keys",placeholder="Paste Elevenlabs",type='password'),165 gr.Textbox(lines=4, label=" Please input the text you wish to generate in order to make the photo speak.", placeholder="English Text here"),166 gr.Dropdown(choices=get_voice_names(), label="Select a voice"),167 gr.Image(label="photo of a Person", type="filepath")168 ], outputs="video",title="Bring your images to life with the talking animation feature now!",examples=examples,cache_examples=False)169 170demo.launch()