|
import spaces |
|
import torch |
|
|
|
import gradio as gr |
|
from transformers import pipeline |
|
from huggingface_hub import InferenceClient |
|
import tempfile |
|
import os |
|
|
|
MODEL_NAME = "openai/whisper-large-v3-turbo" |
|
BATCH_SIZE = 8 |
|
FILE_LIMIT_MB = 1000 |
|
|
|
device = 0 if torch.cuda.is_available() else "cpu" |
|
|
|
pipe = pipeline( |
|
task="automatic-speech-recognition", |
|
model=MODEL_NAME, |
|
chunk_length_s=30, |
|
device=device, |
|
) |
|
|
|
|
|
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN")) |
|
|
|
@spaces.GPU |
|
def transcribe_summarize_and_blog(inputs, task): |
|
if inputs is None: |
|
raise gr.Error("μ€λμ€ νμΌμ΄ μ μΆλμ§ μμμ΅λλ€! μμ²μ μ μΆνκΈ° μ μ μ€λμ€ νμΌμ μ
λ‘λνκ±°λ λ
Ήμν΄ μ£ΌμΈμ.") |
|
|
|
|
|
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"] |
|
|
|
|
|
try: |
|
summary = hf_client.summarization(text) |
|
except Exception as e: |
|
raise gr.Error(f"μμ½ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {e}") |
|
|
|
|
|
try: |
|
blog_post = hf_client.text_generation( |
|
prompt=f"λ€μ λ΄μ©μ κΈ°λ°μΌλ‘ λΈλ‘κ·Έ ν¬μ€ν
μ μμ±ν΄ μ£ΌμΈμ:\n{text}", |
|
max_length=500, |
|
temperature=0.7 |
|
) |
|
except Exception as e: |
|
raise gr.Error(f"λΈλ‘κ·Έ κΈ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€: {e}") |
|
|
|
return { |
|
"transcribed_text": text, |
|
"summary": summary["summary_text"], |
|
"blog_post": blog_post["generated_text"] |
|
} |
|
|
|
css = """ |
|
footer { |
|
visibility: hidden; |
|
} |
|
""" |
|
|
|
file_transcribe = gr.Interface( |
|
fn=transcribe_summarize_and_blog, |
|
inputs=[ |
|
gr.Audio(sources="upload", type="filepath", label="μ€λμ€ νμΌ"), |
|
gr.Radio(["transcribe", "translate"], label="μμ
", value="transcribe"), |
|
], |
|
outputs=["text", "text", "text"], |
|
title="λ°μμ°κΈ° AI: μμ±μ ν
μ€νΈ λ³ν, μμ½ λ° λΈλ‘κ·Έ ν¬μ€ν
μλ μμ±", |
|
flagging_mode="never", |
|
) |
|
|
|
mf_transcribe = gr.Interface(css=css, |
|
fn=transcribe_summarize_and_blog, |
|
inputs=[ |
|
gr.Audio(sources="microphone", type="filepath"), |
|
gr.Radio(["transcribe", "translate"], label="μμ
", value="transcribe"), |
|
], |
|
outputs=["text", "text", "text"], |
|
title="λ°μμ°κΈ° AI: μμ±μ ν
μ€νΈ λ³ν, μμ½ λ° λΈλ‘κ·Έ ν¬μ€ν
μλ μμ±", |
|
flagging_mode="never", |
|
) |
|
|
|
|
|
|
|
|
|
demo = gr.Blocks(theme="Nymbo/Nymbo_Theme") |
|
|
|
with demo: |
|
gr.TabbedInterface([mf_transcribe, file_transcribe], ["λ§μ΄ν¬", "μ€λμ€ νμΌ"]) |
|
|
|
demo.queue().launch(ssr_mode=False) |
|
|