Spaces:
Runtime error
Runtime error
bhaskartripathi
commited on
Commit
•
a55fe26
1
Parent(s):
f72aaa7
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import requests
|
4 |
+
from gtts import gTTS
|
5 |
+
from moviepy.editor import *
|
6 |
+
from io import BytesIO
|
7 |
+
from tempfile import NamedTemporaryFile
|
8 |
+
from base64 import b64encode
|
9 |
+
|
10 |
+
input_gif_path = "https://i.imgur.com/ork8hoP.gif"
|
11 |
+
#Lucy https://i.imgur.com/RLMkj1P.gif
|
12 |
+
#Lisa "https://i.imgur.com/ork8hoP.gif"
|
13 |
+
|
14 |
+
def get_text_response(prompt,openAI_key):
|
15 |
+
openai.api_key = openAI_key
|
16 |
+
completions = openai.Completion.create(engine="text-davinci-003",prompt=prompt,max_tokens=600,n=1,stop=None,temperature=0.5,)
|
17 |
+
message = completions.choices[0].text.strip()
|
18 |
+
return message
|
19 |
+
|
20 |
+
def text_to_speech(text, output_file):
|
21 |
+
tts = gTTS(text=text, lang="en")
|
22 |
+
tts.save(output_file)
|
23 |
+
|
24 |
+
def chat_and_animate(output_file, user_prompt,openAI_key):
|
25 |
+
# Get audio response from OpenAI API
|
26 |
+
text_response = get_text_response(user_prompt,openAI_key)
|
27 |
+
text_to_speech(text_response, "response.mp3")
|
28 |
+
|
29 |
+
# Get audio duration in milliseconds
|
30 |
+
audio = AudioSegment.from_file("response.mp3")
|
31 |
+
audio_duration = len(audio)
|
32 |
+
|
33 |
+
# Download the input GIF
|
34 |
+
response = requests.get(input_gif_path)
|
35 |
+
|
36 |
+
# Save the input GIF to a temporary file
|
37 |
+
with NamedTemporaryFile(delete=False, suffix=".gif") as temp_gif:
|
38 |
+
temp_gif.write(response.content)
|
39 |
+
|
40 |
+
# Load the input GIF
|
41 |
+
gif_clip = VideoFileClip(temp_gif.name)
|
42 |
+
|
43 |
+
# Calculate the number of loops required to match the audio duration
|
44 |
+
num_loops = audio_duration / (gif_clip.duration * 1000)
|
45 |
+
|
46 |
+
# Duplicate the animated GIF to match the audio duration
|
47 |
+
final_gif = gif_clip.loop(n=int(num_loops))
|
48 |
+
|
49 |
+
# Set the audio to the animated GIF
|
50 |
+
final_video = final_gif.set_audio(AudioFileClip("response.mp3"))
|
51 |
+
|
52 |
+
# Save the final video
|
53 |
+
final_video.write_videofile(output_file, codec="libx264", audio_codec="aac")
|
54 |
+
|
55 |
+
# Clean up the temporary GIF file
|
56 |
+
os.unlink(temp_gif.name)
|
57 |
+
|
58 |
+
# Set up the input and output components for the Gradio app
|
59 |
+
user_prompt = gr.inputs.Textbox(label="Ask me anything", default="Welcome")
|
60 |
+
output_video = gr.outputs.Video()
|
61 |
+
|
62 |
+
def chatbot(user_prompt,openAI_key):
|
63 |
+
if user_prompt.strip() == '':
|
64 |
+
return '[ERROR]: Blank input not allowed.'
|
65 |
+
if openAI_key.strip() == '':
|
66 |
+
return '[ERROR]: Please enter your Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
|
67 |
+
if user_prompt.lower() == "welcome":
|
68 |
+
output_file = "preloaded.mp4"
|
69 |
+
else:
|
70 |
+
output_file = "output.mp4"
|
71 |
+
chat_and_animate(output_file, user_prompt,openAI_key)
|
72 |
+
return output_file
|
73 |
+
|
74 |
+
'''
|
75 |
+
gr.Interface(
|
76 |
+
fn=chatbot,
|
77 |
+
inputs=gr.inputs.Textbox(label="Ask me anything", placeholder="Type your question and press enter"),
|
78 |
+
outputs=gr.outputs.Video(),
|
79 |
+
allow_flagging=False,
|
80 |
+
flagging_options=[],
|
81 |
+
title="Deep talks with Lisa !",
|
82 |
+
description="Ask me anything, and I'll give you an animated response! Type 'Welcome' to load the chat assistant.",
|
83 |
+
theme="compact"
|
84 |
+
).launch()
|
85 |
+
'''
|
86 |
+
|
87 |
+
with gr.Blocks() as demo:
|
88 |
+
gr.Markdown(f'<center><h1>Ask Lisa !</h1></center>')
|
89 |
+
gr.Markdown(f'<span><img src="https://i.imgur.com/ork8hoP.gif" width="100" height="100"></span>')
|
90 |
+
with gr.Row():
|
91 |
+
|
92 |
+
with gr.Group():
|
93 |
+
gr.Markdown(f'<p style="text-align:center">Get your Open AI API key <a href="https://platform.openai.com/account/api-keys">here</a></p>')
|
94 |
+
openAI_key=gr.Textbox(label='Enter your OpenAI API key here')
|
95 |
+
question = gr.Textbox(label='Ask me anything, and I will give you an animated response! Enteryour question here')
|
96 |
+
btn = gr.Button(value='Talk')
|
97 |
+
|
98 |
+
btn.style(full_width=True)
|
99 |
+
with gr.Group():
|
100 |
+
output_video = gr.outputs.Video()
|
101 |
+
btn.click(chatbot, inputs=[question,openAI_key], outputs=[output_video])
|
102 |
+
#openai.api_key = os.getenv('Your_Key_Here')
|
103 |
+
|
104 |
+
demo.launch()
|