badassgi / app.py
openfree's picture
Update app.py
2524c67 verified
raw
history blame
3.2 kB
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,
)
# Hugging Face InferenceClient μ‚¬μš©
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)
summary_text = summary["summary_text"] if summary and "summary_text" in summary else "μš”μ•½ν•  수 μ—†μŠ΅λ‹ˆλ‹€."
except Exception as e:
summary_text = f"μš”μ•½ 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}"
# λΈ”λ‘œκ·Έ ν¬μŠ€νŒ… 생성 μš”μ²­
try:
blog_post = hf_client.text_generation(
prompt=f"λ‹€μŒ λ‚΄μš©μ„ 기반으둜 λΈ”λ‘œκ·Έ ν¬μŠ€νŒ…μ„ μž‘μ„±ν•΄ μ£Όμ„Έμš”:\n{text}",
temperature=0.7
)
blog_post_text = blog_post if isinstance(blog_post, str) else "λΈ”λ‘œκ·Έ ν¬μŠ€νŒ…μ„ 생성할 수 μ—†μŠ΅λ‹ˆλ‹€."
except Exception as e:
blog_post_text = f"λΈ”λ‘œκ·Έ κΈ€ 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}"
return {
"transcribed_text": text,
"summary": summary_text,
"blog_post": blog_post_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 λ³€μˆ˜λ₯Ό Gradio Blocks μ»¨ν…Œμ΄λ„ˆλ‘œ μ •μ˜
demo = gr.Blocks(theme="Nymbo/Nymbo_Theme")
# νƒ­ μˆœμ„œλ₯Ό "μ˜€λ””μ˜€ 파일"이 λ¨Όμ €, "마이크"κ°€ 뒀에 μ˜€λ„λ‘ μ„€μ •
with demo:
gr.TabbedInterface([file_transcribe, mf_transcribe], ["μ˜€λ””μ˜€ 파일", "마이크"])
demo.queue().launch(ssr_mode=False)