Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,48 @@
|
|
| 1 |
-
from flask import Flask, render_template, request
|
| 2 |
-
from PIL import Image
|
| 3 |
-
from io import BytesIO
|
| 4 |
-
import os
|
| 5 |
-
from functions import generate_image_caption, convert_text_to_speech
|
| 6 |
-
import soundfile as sf
|
| 7 |
-
from transformers import AutoTokenizer
|
| 8 |
-
from parler_tts import ParlerTTSForConditionalGeneration
|
| 9 |
-
|
| 10 |
-
app = Flask(__name__)
|
| 11 |
-
|
| 12 |
-
UPLOAD_FOLDER = 'static/uploads'
|
| 13 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 14 |
-
|
| 15 |
-
device = "cpu"
|
| 16 |
-
model_name = "parler-tts/parler_tts_mini_v0.1"
|
| 17 |
-
|
| 18 |
-
model = ParlerTTSForConditionalGeneration.from_pretrained(model_name).to(device)
|
| 19 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
-
|
| 21 |
-
@app.route('/')
|
| 22 |
-
def index():
|
| 23 |
-
return render_template('index.html')
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
@app.route('/', methods=['POST'])
|
| 27 |
-
def upload_image():
|
| 28 |
-
uploaded_file = request.files['image']
|
| 29 |
-
if uploaded_file.filename != '':
|
| 30 |
-
image_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file.filename)
|
| 31 |
-
uploaded_file.save(image_path)
|
| 32 |
-
|
| 33 |
-
caption = generate_image_caption(image_path)
|
| 34 |
-
|
| 35 |
-
audio_arr = convert_text_to_speech(caption)
|
| 36 |
-
sf.write("static/audio.wav", audio_arr, model.config_class.sampling_rate)
|
| 37 |
-
|
| 38 |
-
img = Image.open(image_path)
|
| 39 |
-
img_io = BytesIO()
|
| 40 |
-
img.save(img_io, 'PNG')
|
| 41 |
-
img_encoded = img_io.getvalue().encode('base64')
|
| 42 |
-
|
| 43 |
-
return render_template('result.html', caption=caption, audio_path="audio.wav", image=img_encoded)
|
| 44 |
-
else:
|
| 45 |
-
return render_template('index.html', message="No image uploaded!")
|
| 46 |
-
|
| 47 |
-
if __name__ == '__main__':
|
| 48 |
-
app.run(debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
import os
|
| 5 |
+
from functions import generate_image_caption, convert_text_to_speech
|
| 6 |
+
import soundfile as sf
|
| 7 |
+
from transformers import AutoTokenizer
|
| 8 |
+
from parler_tts import ParlerTTSForConditionalGeneration
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
UPLOAD_FOLDER = 'static/uploads'
|
| 13 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 14 |
+
|
| 15 |
+
device = "cpu"
|
| 16 |
+
model_name = "parler-tts/parler_tts_mini_v0.1"
|
| 17 |
+
|
| 18 |
+
model = ParlerTTSForConditionalGeneration.from_pretrained(model_name, trust_remote_code=True).to(device)
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 20 |
+
|
| 21 |
+
@app.route('/')
|
| 22 |
+
def index():
|
| 23 |
+
return render_template('index.html')
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@app.route('/', methods=['POST'])
|
| 27 |
+
def upload_image():
|
| 28 |
+
uploaded_file = request.files['image']
|
| 29 |
+
if uploaded_file.filename != '':
|
| 30 |
+
image_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file.filename)
|
| 31 |
+
uploaded_file.save(image_path)
|
| 32 |
+
|
| 33 |
+
caption = generate_image_caption(image_path)
|
| 34 |
+
|
| 35 |
+
audio_arr = convert_text_to_speech(caption)
|
| 36 |
+
sf.write("static/audio.wav", audio_arr, model.config_class.sampling_rate)
|
| 37 |
+
|
| 38 |
+
img = Image.open(image_path)
|
| 39 |
+
img_io = BytesIO()
|
| 40 |
+
img.save(img_io, 'PNG')
|
| 41 |
+
img_encoded = img_io.getvalue().encode('base64')
|
| 42 |
+
|
| 43 |
+
return render_template('result.html', caption=caption, audio_path="audio.wav", image=img_encoded)
|
| 44 |
+
else:
|
| 45 |
+
return render_template('index.html', message="No image uploaded!")
|
| 46 |
+
|
| 47 |
+
if __name__ == '__main__':
|
| 48 |
+
app.run(debug=True)
|