matanmichaely commited on
Commit
d1b5c08
·
1 Parent(s): 1ff7568

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import streamlit as st
3
+ import os
4
+
5
+
6
+
7
+ # img2text
8
+ def img_to_text(url):
9
+ image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
10
+
11
+ text = image_to_text(url)[0]["generated_text"]
12
+ return text
13
+
14
+
15
+ # llm
16
+ def generate_story(text):
17
+ generator = pipeline("text-generation", model="distilgpt2")
18
+
19
+ result = generator(text, max_length=20, num_return_sequences=1)
20
+ return result[0]['generated_text']
21
+
22
+
23
+ #
24
+ # text-to-speech
25
+ def text_to_speech(text):
26
+ import requests
27
+
28
+ API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
29
+ headers = {"Authorization": f"Bearer {os.environ.get('HUGGINGFACE_API_TOKEN')}"}
30
+ payload = {
31
+ "inputs": text
32
+ }
33
+
34
+ response = requests.post(API_URL, headers=headers, json=payload)
35
+ response.raise_for_status()
36
+ with open('audio.flac', 'wb') as file:
37
+ file.write(response.content)
38
+
39
+
40
+
41
+ def main():
42
+ st.set_page_config(page_title="img to audio story")
43
+ st.header("turn image to audio story")
44
+ uploaded_file = st.file_uploader("Choose an image ... ", type="jpg")
45
+
46
+ if uploaded_file is not None:
47
+ print(uploaded_file)
48
+ bytes_data = uploaded_file.getvalue()
49
+ with open(uploaded_file.name, "wb") as file:
50
+ file.write(bytes_data)
51
+ st.image(uploaded_file, caption="Uploaded image", use_column_width=True)
52
+ text = img_to_text(uploaded_file.name)
53
+ story = generate_story(text)
54
+ text_to_speech(story)
55
+
56
+ with st.expander("text"):
57
+ st.write(text)
58
+ with st.expander("story"):
59
+ st.write(story)
60
+ st.audio("audio.flac")
61
+