openfree commited on
Commit
a5099f1
Β·
verified Β·
1 Parent(s): f444932

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -12
app.py CHANGED
@@ -3,7 +3,7 @@ import torch
3
 
4
  import gradio as gr
5
  from transformers import pipeline
6
-
7
  import tempfile
8
  import os
9
 
@@ -20,15 +20,38 @@ pipe = pipeline(
20
  device=device,
21
  )
22
 
 
 
23
 
24
  @spaces.GPU
25
- def transcribe(inputs, task):
26
  if inputs is None:
27
  raise gr.Error("μ˜€λ””μ˜€ 파일이 μ œμΆœλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€! μš”μ²­μ„ μ œμΆœν•˜κΈ° 전에 μ˜€λ””μ˜€ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜κ±°λ‚˜ λ…ΉμŒν•΄ μ£Όμ„Έμš”.")
28
 
 
29
  text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
30
- return text
31
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  css = """
34
  footer {
@@ -37,25 +60,25 @@ footer {
37
  """
38
 
39
  mf_transcribe = gr.Interface(css=css,
40
- fn=transcribe,
41
  inputs=[
42
  gr.Audio(sources="microphone", type="filepath"),
43
  gr.Radio(["transcribe", "translate"], label="μž‘μ—…", value="transcribe"),
44
  ],
45
- outputs="text",
46
- title="λ°›μ•„μ“°κΈ° AI: μŒμ„±μ„ ν…μŠ€νŠΈλ‘œ λ³€ν™˜",
47
- flagging_mode="never", # 더 이상 μ‚¬μš©λ˜μ§€ μ•ŠλŠ” allow_flagging을 flagging_mode둜 λ³€κ²½
48
  )
49
 
50
  file_transcribe = gr.Interface(
51
- fn=transcribe,
52
  inputs=[
53
  gr.Audio(sources="upload", type="filepath", label="μ˜€λ””μ˜€ 파일"),
54
  gr.Radio(["transcribe", "translate"], label="μž‘μ—…", value="transcribe"),
55
  ],
56
- outputs="text",
57
- title="Whisper Large V3 Turbo: μŒμ„±μ„ ν…μŠ€νŠΈλ‘œ λ³€ν™˜",
58
- flagging_mode="never", # 더 이상 μ‚¬μš©λ˜μ§€ μ•ŠλŠ” allow_flagging을 flagging_mode둜 λ³€κ²½
59
  )
60
 
61
  # demo λ³€μˆ˜λ₯Ό Gradio Blocks μ»¨ν…Œμ΄λ„ˆλ‘œ μ •μ˜
 
3
 
4
  import gradio as gr
5
  from transformers import pipeline
6
+ from huggingface_hub import InferenceClient
7
  import tempfile
8
  import os
9
 
 
20
  device=device,
21
  )
22
 
23
+ # Hugging Face InferenceClient μ‚¬μš©
24
+ hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
25
 
26
  @spaces.GPU
27
+ def transcribe_summarize_and_blog(inputs, task):
28
  if inputs is None:
29
  raise gr.Error("μ˜€λ””μ˜€ 파일이 μ œμΆœλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€! μš”μ²­μ„ μ œμΆœν•˜κΈ° 전에 μ˜€λ””μ˜€ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜κ±°λ‚˜ λ…ΉμŒν•΄ μ£Όμ„Έμš”.")
30
 
31
+ # μŒμ„±μ„ ν…μŠ€νŠΈλ‘œ λ³€ν™˜
32
  text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
33
+
34
+ # λ³€ν™˜λœ ν…μŠ€νŠΈ μš”μ•½ μš”μ²­
35
+ try:
36
+ summary = hf_client.summarization(text)
37
+ except Exception as e:
38
+ raise gr.Error(f"μš”μ•½ 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}")
39
+
40
+ # λΈ”λ‘œκ·Έ ν¬μŠ€νŒ… 생성 μš”μ²­
41
+ try:
42
+ blog_post = hf_client.text_generation(
43
+ prompt=f"λ‹€μŒ λ‚΄μš©μ„ 기반으둜 λΈ”λ‘œκ·Έ ν¬μŠ€νŒ…μ„ μž‘μ„±ν•΄ μ£Όμ„Έμš”:\n{text}",
44
+ max_length=500,
45
+ temperature=0.7
46
+ )
47
+ except Exception as e:
48
+ raise gr.Error(f"λΈ”λ‘œκ·Έ κΈ€ 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}")
49
+
50
+ return {
51
+ "transcribed_text": text,
52
+ "summary": summary["summary_text"],
53
+ "blog_post": blog_post["generated_text"]
54
+ }
55
 
56
  css = """
57
  footer {
 
60
  """
61
 
62
  mf_transcribe = gr.Interface(css=css,
63
+ fn=transcribe_summarize_and_blog,
64
  inputs=[
65
  gr.Audio(sources="microphone", type="filepath"),
66
  gr.Radio(["transcribe", "translate"], label="μž‘μ—…", value="transcribe"),
67
  ],
68
+ outputs=["text", "text", "text"], # λ³€ν™˜λœ ν…μŠ€νŠΈ, μš”μ•½, λΈ”λ‘œκ·Έ κΈ€ 좜λ ₯
69
+ title="λ°›μ•„μ“°κΈ° AI: μŒμ„±μ„ ν…μŠ€νŠΈ λ³€ν™˜, μš”μ•½ 및 λΈ”λ‘œκ·Έ ν¬μŠ€νŒ… μžλ™ 생성",
70
+ flagging_mode="never",
71
  )
72
 
73
  file_transcribe = gr.Interface(
74
+ fn=transcribe_summarize_and_blog,
75
  inputs=[
76
  gr.Audio(sources="upload", type="filepath", label="μ˜€λ””μ˜€ 파일"),
77
  gr.Radio(["transcribe", "translate"], label="μž‘μ—…", value="transcribe"),
78
  ],
79
+ outputs=["text", "text", "text"], # λ³€ν™˜λœ ν…μŠ€νŠΈ, μš”μ•½, λΈ”λ‘œκ·Έ κΈ€ 좜λ ₯
80
+ title="λ°›μ•„μ“°κΈ° AI: μŒμ„±μ„ ν…μŠ€νŠΈ λ³€ν™˜, μš”μ•½ 및 λΈ”λ‘œκ·Έ ν¬μŠ€νŒ… μžλ™ 생성",
81
+ flagging_mode="never",
82
  )
83
 
84
  # demo λ³€μˆ˜λ₯Ό Gradio Blocks μ»¨ν…Œμ΄λ„ˆλ‘œ μ •μ˜