burask commited on
Commit
32a8fa2
·
verified ·
1 Parent(s): 11916e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -82
app.py CHANGED
@@ -1,82 +1,82 @@
1
- from flask import Flask, render_template, request
2
- import os
3
- import subprocess
4
- from speech_recognition import Recognizer, AudioFile
5
- from werkzeug.utils import secure_filename
6
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
7
- from deepmultilingualpunctuation import PunctuationModel
8
-
9
- app = Flask(__name__)
10
- yuklenen_dosyalar = "uploads"
11
- app.config["yuklenen_dosyalar"] = yuklenen_dosyalar
12
- dosya_turleri = {"mp3", "m4a"}
13
-
14
- # Modelleri yükle
15
- tokenizer = AutoTokenizer.from_pretrained("csebuetnlp/mT5_multilingual_XLSum")
16
- model = AutoModelForSeq2SeqLM.from_pretrained("csebuetnlp/mT5_multilingual_XLSum")
17
- punct_model = PunctuationModel(model="oliverguhr/fullstop-punctuation-multilingual-sonar-base")
18
-
19
- def metin_duzenleme(text):
20
- text = text.strip()
21
- try:
22
- return punct_model.restore_punctuation(text)
23
- except Exception as e:
24
- print("Noktalama hatası:", e)
25
- return text
26
-
27
- def metin_ozetleme(text):
28
- girilen_metin = "tr: " + text
29
- girilenler = tokenizer([girilen_metin], return_tensors="pt", max_length=10000, truncation=True)
30
- summary_ids = model.generate(
31
- girilenler["input_ids"],
32
- max_length=1000,
33
- min_length=1,
34
- length_penalty=1.0,
35
- num_beams=4,
36
- early_stopping=True
37
- )
38
- return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
39
-
40
- def uygun_turler(filename):
41
- return "." in filename and filename.rsplit(".", 1)[1].lower() in dosya_turleri
42
-
43
- def convert_mp3_to_wav(mp3_path):
44
- wav_path = mp3_path.rsplit('.', 1)[0] + '.wav'
45
- subprocess.run(['ffmpeg', '-i', mp3_path, wav_path, '-y'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
46
- return wav_path
47
-
48
- def ses_donusturucu(wav_path):
49
- recognizer = Recognizer()
50
- with AudioFile(wav_path) as source:
51
- ses = recognizer.record(source)
52
- try:
53
- return recognizer.recognize_google(ses, language="tr-TR")
54
- except Exception as e:
55
- return f"Ses tanıma hatası: {e}"
56
-
57
- @app.route("/", methods=["GET", "POST"])
58
- def index():
59
- if request.method == "POST":
60
- if "file" not in request.files:
61
- return "Dosya bulunamadı."
62
- file = request.files["file"]
63
- if file.filename == "":
64
- return "Dosya seçilmedi."
65
- if file and uygun_turler(file.filename):
66
- filename = secure_filename(file.filename)
67
- file_path = os.path.join(app.config["yuklenen_dosyalar"], filename)
68
- file.save(file_path)
69
-
70
- wav_path = convert_mp3_to_wav(file_path)
71
- raw_text = ses_donusturucu(wav_path)
72
- metin = metin_duzenleme(raw_text)
73
- ozet = metin_ozetleme(metin)
74
-
75
- return render_template("index.html", metin=metin, ozet=ozet)
76
- return render_template("index.html")
77
-
78
- if __name__ == "__main__":
79
- if not os.path.exists(yuklenen_dosyalar):
80
- os.makedirs(yuklenen_dosyalar)
81
- port = int(os.environ.get("PORT", 7860)) # Hugging Face için 7860 port
82
- app.run(host="0.0.0.0", port=port, debug=False)
 
1
+ from flask import Flask, render_template, request
2
+ import os
3
+ import subprocess
4
+ from speech_recognition import Recognizer, AudioFile
5
+ from werkzeug.utils import secure_filename
6
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
7
+ from deepmultilingualpunctuation import PunctuationModel
8
+
9
+ app = Flask(__name__)
10
+ yuklenen_dosyalar = "uploads"
11
+ app.config["yuklenen_dosyalar"] = yuklenen_dosyalar
12
+ dosya_turleri = {"mp3", "m4a"}
13
+
14
+ # Modelleri yükle
15
+ tokenizer = AutoTokenizer.from_pretrained("csebuetnlp/mT5_multilingual_XLSum", use_fast=False)
16
+ model = AutoModelForSeq2SeqLM.from_pretrained("csebuetnlp/mT5_multilingual_XLSum")
17
+ punct_model = PunctuationModel(model="oliverguhr/fullstop-punctuation-multilingual-sonar-base")
18
+
19
+ def metin_duzenleme(text):
20
+ text = text.strip()
21
+ try:
22
+ return punct_model.restore_punctuation(text)
23
+ except Exception as e:
24
+ print("Noktalama hatası:", e)
25
+ return text
26
+
27
+ def metin_ozetleme(text):
28
+ girilen_metin = "tr: " + text
29
+ girilenler = tokenizer([girilen_metin], return_tensors="pt", max_length=10000, truncation=True)
30
+ summary_ids = model.generate(
31
+ girilenler["input_ids"],
32
+ max_length=1000,
33
+ min_length=1,
34
+ length_penalty=1.0,
35
+ num_beams=4,
36
+ early_stopping=True
37
+ )
38
+ return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
39
+
40
+ def uygun_turler(filename):
41
+ return "." in filename and filename.rsplit(".", 1)[1].lower() in dosya_turleri
42
+
43
+ def convert_mp3_to_wav(mp3_path):
44
+ wav_path = mp3_path.rsplit('.', 1)[0] + '.wav'
45
+ subprocess.run(['ffmpeg', '-i', mp3_path, wav_path, '-y'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
46
+ return wav_path
47
+
48
+ def ses_donusturucu(wav_path):
49
+ recognizer = Recognizer()
50
+ with AudioFile(wav_path) as source:
51
+ ses = recognizer.record(source)
52
+ try:
53
+ return recognizer.recognize_google(ses, language="tr-TR")
54
+ except Exception as e:
55
+ return f"Ses tanıma hatası: {e}"
56
+
57
+ @app.route("/", methods=["GET", "POST"])
58
+ def index():
59
+ if request.method == "POST":
60
+ if "file" not in request.files:
61
+ return "Dosya bulunamadı."
62
+ file = request.files["file"]
63
+ if file.filename == "":
64
+ return "Dosya seçilmedi."
65
+ if file and uygun_turler(file.filename):
66
+ filename = secure_filename(file.filename)
67
+ file_path = os.path.join(app.config["yuklenen_dosyalar"], filename)
68
+ file.save(file_path)
69
+
70
+ wav_path = convert_mp3_to_wav(file_path)
71
+ raw_text = ses_donusturucu(wav_path)
72
+ metin = metin_duzenleme(raw_text)
73
+ ozet = metin_ozetleme(metin)
74
+
75
+ return render_template("index.html", metin=metin, ozet=ozet)
76
+ return render_template("index.html")
77
+
78
+ if __name__ == "__main__":
79
+ if not os.path.exists(yuklenen_dosyalar):
80
+ os.makedirs(yuklenen_dosyalar)
81
+ port = int(os.environ.get("PORT", 7860)) # Hugging Face için 7860 port
82
+ app.run(host="0.0.0.0", port=port, debug=False)