Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import gradio as gr
|
|
| 4 |
import pandas as pd
|
| 5 |
import os
|
| 6 |
import spaces
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# تنظیم کلید API از متغیر محیطی
|
| 9 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
@@ -13,10 +15,20 @@ whisper = pipeline("automatic-speech-recognition", model="openai/whisper-large-v
|
|
| 13 |
|
| 14 |
@spaces.GPU
|
| 15 |
def speech_to_text(audio_file):
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def summarize_text(text):
|
|
|
|
|
|
|
| 20 |
response = client.chat.completions.create(
|
| 21 |
model="gpt-3.5-turbo",
|
| 22 |
messages=[
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import os
|
| 6 |
import spaces
|
| 7 |
+
import soundfile as sf
|
| 8 |
+
import numpy as np
|
| 9 |
|
| 10 |
# تنظیم کلید API از متغیر محیطی
|
| 11 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
|
| 15 |
|
| 16 |
@spaces.GPU
|
| 17 |
def speech_to_text(audio_file):
|
| 18 |
+
if audio_file is None:
|
| 19 |
+
return "خطا: فایل صوتی دریافت نشد. لطفاً یک فایل صوتی آپلود کنید."
|
| 20 |
+
try:
|
| 21 |
+
# خواندن فایل صوتی
|
| 22 |
+
audio_data, sample_rate = sf.read(audio_file)
|
| 23 |
+
# تبدیل به فرمت مورد نیاز Whisper
|
| 24 |
+
result = whisper(audio_data, sampling_rate=sample_rate)
|
| 25 |
+
return result["text"]
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"خطا در پردازش فایل صوتی: {str(e)}"
|
| 28 |
|
| 29 |
def summarize_text(text):
|
| 30 |
+
if not text or text.startswith("خطا"):
|
| 31 |
+
return "خطا: متن معتبر برای خلاصهسازی دریافت نشد."
|
| 32 |
response = client.chat.completions.create(
|
| 33 |
model="gpt-3.5-turbo",
|
| 34 |
messages=[
|