File size: 3,545 Bytes
6d78a9a
 
 
 
 
b9ff660
97c148b
 
f0889c4
6d78a9a
21999b7
6d78a9a
 
21999b7
b9ff660
6d78a9a
b9ff660
6d78a9a
97c148b
 
 
 
 
97fb835
 
f0889c4
21999b7
 
97c148b
 
 
6d78a9a
 
97c148b
 
6d78a9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b66d033
 
b448bdb
b66d033
 
 
6d78a9a
b448bdb
6d78a9a
b448bdb
6d78a9a
 
 
4e2b9b1
b66d033
1497ae5
 
 
b66d033
 
6d78a9a
b66d033
6d78a9a
 
 
b9ff660
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
from transformers import pipeline
from openai import OpenAI
import gradio as gr
import pandas as pd
import os
import spaces
import soundfile as sf
import numpy as np
import librosa

# تنظیم کلید API از متغیر محیطی
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# بارگذاری مدل Whisper
whisper = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3-turbo", device="cuda")

@spaces.GPU
def speech_to_text(audio_file):
    if audio_file is None:
        return "خطا: فایل صوتی دریافت نشد. لطفاً یک فایل صوتی آپلود کنید."
    try:
        # خواندن فایل صوتی
        audio_data, sample_rate = sf.read(audio_file)
        # اطمینان از نرخ نمونه‌برداری 16kHz (استاندارد برای Whisper)
        if sample_rate != 16000:
            audio_data = librosa.resample(audio_data, orig_sr=sample_rate, target_sr=16000)
        # پردازش با Whisper با پشتیبانی از فایل‌های طولانی
        result = whisper(audio_data, return_timestamps=True)
        return result["text"]
    except Exception as e:
        return f"خطا در پردازش فایل صوتی: {str(e)}"

def summarize_text(text):
    if not text or text.startswith("خطا"):
        return "خطا: متن معتبر برای خلاصه‌سازی دریافت نشد."
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a helpful assistant that summarizes text in Persian."},
            {"role": "user", "content": f"لطفاً متن زیر را به صورت خلاصه و در قالب یک گزارش کوتاه ارائه دهید:\n{text}"}
        ],
        max_tokens=150
    )
    return response.choices[0].message.content

def create_summary_table(summary):
    data = {"خلاصه گزارش": [summary]}
    df = pd.DataFrame(data)
    return df

def process_audio(audio_file, progress=gr.Progress()):
    if audio_file is None:
        return "خطا: فایل صوتی دریافت نشد.", "", None
    progress(0, desc="در حال پردازش فایل صوتی...")
    text = speech_to_text(audio_file)
    progress(0.5, desc="در حال خلاصه‌سازی متن...")
    summary = summarize_text(text)
    progress(0.9, desc="ایجاد جدول خلاصه...")
    table = create_summary_table(summary)
    progress(1.0, desc="پردازش کامل شد")
    return text, summary, table

with gr.Blocks() as app:
    gr.Markdown("## اپلیکیشن تبدیل صوت به متن و خلاصه سازی کال سنتر\n**توجه**: برای بهترین عملکرد، از فایل‌های صوتی کوتاه (کمتر از ۵ مگابایت، ۱۰-۲۰ ثانیه) با فرمت MP3، WAV یا M4A استفاده کنید. فایل‌های طولانی‌تر از ۳۰ ثانیه ممکن است زمان پردازش بیشتری نیاز داشته باشند.")
    audio_input = gr.Audio(type="filepath", label="فایل صوتی را آپلود کنید (MP3، WAV، M4A)")
    text_output = gr.Textbox(label="متن تبدیل‌شده")
    summary_output = gr.Textbox(label="خلاصه گزارش")
    table_output = gr.DataFrame(label="جدول خلاصه")
    submit_button = gr.Button("پردازش")
    submit_button.click(
        fn=process_audio,
        inputs=audio_input,
        outputs=[text_output, summary_output, table_output]
    )

app.launch()