Spaces:
PlayHT
/
Running on CPU Upgrade

File size: 5,445 Bytes
69617e8
f16d803
 
0b47c5d
 
69617e8
1101b16
f16d803
 
 
 
 
1101b16
69617e8
 
 
 
b326bed
 
1101b16
0b47c5d
69617e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f16d803
69617e8
f16d803
69617e8
 
 
99f3aa9
 
 
69617e8
99f3aa9
69617e8
99f3aa9
1101b16
99f3aa9
 
1101b16
99f3aa9
1101b16
99f3aa9
 
1101b16
 
99f3aa9
1101b16
 
f16d803
 
0b47c5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f16d803
1101b16
0b47c5d
f16d803
 
 
0b47c5d
f16d803
 
 
 
 
69617e8
 
f16d803
 
 
69617e8
f16d803
 
1101b16
69617e8
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import tempfile
import shutil
import numpy as np
import requests
import google.generativeai as genai
import gradio as gr
import subprocess
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import PIL.Image
from gradio import processing_utils, utils

# Configure Google Gemini API
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))

# Play.ht API keys
API_KEY = os.getenv('PLAY_API_KEY')
USER_ID = os.getenv('PLAY_USER_ID')

# Function to upload image to Gemini and get roasted text
def upload_to_gemini(path, mime_type="image/jpeg"):
    file = genai.upload_file(path, mime_type=mime_type)
    return file

def generate_roast(image_path):
    uploaded_file = upload_to_gemini(image_path)
    generation_config = {
        "temperature": 1,
        "top_p": 0.95,
        "top_k": 40,
        "max_output_tokens": 8192,
        "response_mime_type": "text/plain",
    }
    model = genai.GenerativeModel(
        model_name="gemini-1.5-flash-002",
        generation_config=generation_config,
        system_instruction="You are a professional satirist and fashion expert. Roast the profile picture.",
    )
    chat_session = model.start_chat(history=[{"role": "user", "parts": [uploaded_file]}])
    response = chat_session.send_message("Roast this image!")
    return response.text

def text_to_speech(text):
    url = "https://api.play.ht/api/v2/tts/stream"
    payload = {
        "voice": "s3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json",
        "output_format": "mp3",
        "text": text,
    }
    headers = {
        "accept": "audio/mpeg",
        "content-type": "application/json",
        "Authorization": API_KEY,
        "X-User-ID": USER_ID
    }
    response = requests.post(url, json=payload, headers=headers)
    if response.status_code == 200:
        audio_path = "output_audio.mp3"
        with open(audio_path, "wb") as audio_file:
            audio_file.write(response.content)
        return audio_path
    else:
        raise ValueError(f"Error: {response.status_code} - {response.text}")

# Generate waveform
def make_waveform(
    audio, 
    bg_color="#f3f4f6", 
    bg_image=None, 
    fg_alpha=0.75, 
    bars_color=("#fbbf24", "#ea580c"), 
    bar_count=50, 
    bar_width=0.6, 
    animate=False
):
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation
    import tempfile
    import shutil
    import PIL.Image

    if isinstance(audio, str):
        audio = processing_utils.audio_from_file(audio)
    
    duration = round(len(audio[1]) / audio[0], 4)
    samples = audio[1]
    if len(samples.shape) > 1:
        samples = np.mean(samples, 1)
    bins_to_pad = bar_count - (len(samples) % bar_count)
    samples = np.pad(samples, [(0, bins_to_pad)])
    samples = np.reshape(samples, (bar_count, -1))
    samples = np.abs(samples)
    samples = np.max(samples, 1)
    
    # Color gradient for bars
    def hex_to_rgb(hex_str):
        return [int(hex_str[i : i + 2], 16) for i in range(1, 6, 2)]

    def get_color_gradient(c1, c2, n):
        c1_rgb = np.array(hex_to_rgb(c1)) / 255
        c2_rgb = np.array(hex_to_rgb(c2)) / 255
        mix_pcts = [x / (n - 1) for x in range(n)]
        rgb_colors = [((1 - mix) * c1_rgb + (mix * c2_rgb)) for mix in mix_pcts]
        return [
            "#" + "".join(f"{int(round(val * 255)):02x}" for val in item)
            for item in rgb_colors
        ]
    
    color = (
        bars_color
        if isinstance(bars_color, str)
        else get_color_gradient(bars_color[0], bars_color[1], bar_count)
    )

    fig, ax = plt.subplots(figsize=(5, 1), dpi=200, frameon=False)
    fig.subplots_adjust(left=0, bottom=0, right=1, top=1)
    plt.axis("off")
    plt.margins(x=0)

    barcollection = ax.bar(
        np.arange(0, bar_count),
        samples * 2,
        bottom=(-1 * samples),
        width=bar_width,
        color=color,
        alpha=fg_alpha,
    )
    
    # Temporary output file
    tmp_img = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
    savefig_kwargs = {"facecolor": bg_color} if bg_image is None else {"transparent": True}
    plt.savefig(tmp_img.name, **savefig_kwargs)

    # Use ffmpeg to create video
    output_video_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
    ffmpeg_cmd = [
        shutil.which("ffmpeg"),
        "-loop", "1",
        "-i", tmp_img.name,
        "-i", audio,
        "-c:v", "libx264",
        "-c:a", "aac",
        "-shortest",
        "-y",
        output_video_path,
    ]
    subprocess.run(ffmpeg_cmd, check=True)
    return output_video_path

# Full Gradio Interface Function
def process_image(image):
    roast_text = generate_roast(image)
    audio_path = text_to_speech(roast_text)
    final_video_path = make_waveform(audio_path, bg_image=image, animate=True)
    return roast_text, final_video_path

# Gradio Blocks UI
with gr.Blocks() as demo:
    gr.Markdown("# Image Roast and Waveform Video Generator")
    
    with gr.Row():
        image_input = gr.Image(type="filepath", label="Upload Image")
        output_text = gr.Textbox(label="Roast Text")
        output_video = gr.Video(label="Roast Waveform Video")
    
    submit_button = gr.Button("Generate Roast Video")
    submit_button.click(process_image, inputs=image_input, outputs=[output_text, output_video])

# Launch the app
demo.launch(debug=True)