Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
import spacy
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# ่จญ็ฝฎ่จญๅ
|
| 8 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 10 |
+
|
| 11 |
+
# Whisper ๆจกๅๅๅงๅ๏ผ่ช้ณ่ฝๆๅญ๏ผ
|
| 12 |
+
whisper_model_id = "openai/whisper-large-v3"
|
| 13 |
+
whisper_model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 14 |
+
whisper_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
| 15 |
+
)
|
| 16 |
+
whisper_model.to(device)
|
| 17 |
+
whisper_processor = AutoProcessor.from_pretrained(whisper_model_id)
|
| 18 |
+
|
| 19 |
+
whisper_pipe = pipeline(
|
| 20 |
+
"automatic-speech-recognition",
|
| 21 |
+
model=whisper_model,
|
| 22 |
+
tokenizer=whisper_processor.tokenizer,
|
| 23 |
+
feature_extractor=whisper_processor.feature_extractor,
|
| 24 |
+
device=device,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# DeepSeek ๆจกๅๅๅงๅ๏ผๆๆฌ็ๆ๏ผ
|
| 28 |
+
deepseek_pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-R1", trust_remote_code=True)
|
| 29 |
+
|
| 30 |
+
# spaCy ๅๅงๅ๏ผๆๆฌๅ้ก่ๆจ็ฑค๏ผ
|
| 31 |
+
nlp = spacy.load("en_core_web_sm")
|
| 32 |
+
|
| 33 |
+
# ๅฎ็พฉ่็ๅฝๆธ
|
| 34 |
+
def process_audio(audio_file):
|
| 35 |
+
# ่ช้ณ่ฝๆๅญ
|
| 36 |
+
result = whisper_pipe(audio_file)["text"]
|
| 37 |
+
|
| 38 |
+
# ไฝฟ็จ DeepSeek ็ๆๅๆ
|
| 39 |
+
messages = [{"role": "user", "content": result}]
|
| 40 |
+
deepseek_response = deepseek_pipe(messages)[0]["generated_text"]
|
| 41 |
+
|
| 42 |
+
# ไฝฟ็จ spaCy ๅๆๆๆฌ
|
| 43 |
+
doc = nlp(deepseek_response)
|
| 44 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 45 |
+
|
| 46 |
+
return result, deepseek_response, entities
|
| 47 |
+
|
| 48 |
+
# Gradio ็้ข่จญ่จ
|
| 49 |
+
def interface(audio_file):
|
| 50 |
+
transcription, response, entities = process_audio(audio_file)
|
| 51 |
+
return {
|
| 52 |
+
"Transcription (Whisper)": transcription,
|
| 53 |
+
"AI Response (DeepSeek)": response,
|
| 54 |
+
"Extracted Entities (spaCy)": entities,
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# Gradio ๆ็จ็จๅบ
|
| 58 |
+
with gr.Blocks() as app:
|
| 59 |
+
gr.Markdown("# AI ๅฎขๆ่ชๅๅ็ณป็ตฑ")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="ไธๅณ่ช้ณ")
|
| 63 |
+
output_text = gr.JSON(label="็ตๆ")
|
| 64 |
+
|
| 65 |
+
submit_button = gr.Button("ๆไบค")
|
| 66 |
+
submit_button.click(fn=interface, inputs=audio_input, outputs=output_text)
|
| 67 |
+
|
| 68 |
+
# ๅๅๆ็จ็จๅบ๏ผๆฌๅฐๆธฌ่ฉฆๆไฝฟ็จ๏ผ
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
app.launch()
|
| 71 |
+
|
| 72 |
+
# ้จ็ฝฒๅฐ Hugging Face Spaces ๆ๏ผๅฐ `app.launch()` ๆฟๆ็บ `app`
|