new commit
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
os.environ['Hugging_face']='Hugging_face'
|
6 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv("Hugging_face")
|
7 |
+
|
8 |
+
os.environ['OPENAI_API_KEY']='openAPI'
|
9 |
+
|
10 |
+
import streamlit as st
|
11 |
+
import tempfile
|
12 |
+
|
13 |
+
|
14 |
+
#Image to Text Generation
|
15 |
+
def img2text(url):
|
16 |
+
image_to_text = pipeline('image-to-text', model="Salesforce/blip-image-captioning-base", max_new_tokens=100)
|
17 |
+
text = image_to_text(url)
|
18 |
+
|
19 |
+
# print(text[0]["generated_text"])
|
20 |
+
return text[0]["generated_text"]
|
21 |
+
|
22 |
+
## Text to Story Generation
|
23 |
+
#####################################################
|
24 |
+
from langchain.chains import LLMChain
|
25 |
+
from langchain.llms import OpenAI
|
26 |
+
from langchain.prompts import PromptTemplate
|
27 |
+
|
28 |
+
def generate_story(scenario):
|
29 |
+
template= """
|
30 |
+
You are a story teller
|
31 |
+
You can generate a short story based on a simple narrative, the story shoule be no more than 100 words:
|
32 |
+
|
33 |
+
CONTEXT: {scenario}
|
34 |
+
STORY:
|
35 |
+
"""
|
36 |
+
prompt = PromptTemplate(
|
37 |
+
input_variables=["scenario"],
|
38 |
+
template=template,
|
39 |
+
)
|
40 |
+
|
41 |
+
chain = LLMChain(llm=OpenAI(temperature=1), prompt=prompt)
|
42 |
+
|
43 |
+
story = chain.run(scenario)
|
44 |
+
# print(story)
|
45 |
+
return story
|
46 |
+
|
47 |
+
## Story to Speech Generation
|
48 |
+
##########################################
|
49 |
+
def text2speech(message):
|
50 |
+
|
51 |
+
API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
|
52 |
+
headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
|
53 |
+
|
54 |
+
payloads = {
|
55 |
+
"inputs": message
|
56 |
+
}
|
57 |
+
|
58 |
+
response = requests.post(API_URL, headers=headers, json=payloads)
|
59 |
+
|
60 |
+
with open('audio.mp3', 'wb') as file:
|
61 |
+
file.write(response.content)
|
62 |
+
|
63 |
+
|
64 |
+
## Integration with streamlit
|
65 |
+
def main():
|
66 |
+
st.header("Turn _Images_ into Audio :red[Stories]")
|
67 |
+
|
68 |
+
uploaded_file = st.file_uploader("Choose an image..", type='jpg')
|
69 |
+
|
70 |
+
if uploaded_file is not None:
|
71 |
+
bytes_data = uploaded_file.getvalue()
|
72 |
+
with tempfile.NamedTemporaryFile(delete=False) as file:
|
73 |
+
file.write(bytes_data)
|
74 |
+
file_path = file.name
|
75 |
+
|
76 |
+
st.image(uploaded_file, caption='Uploaded Image',use_column_width=True)
|
77 |
+
|
78 |
+
scenario = img2text(file_path)
|
79 |
+
story = generate_story(scenario)
|
80 |
+
text2speech(story)
|
81 |
+
|
82 |
+
with st.expander("Scenario"):
|
83 |
+
st.write(scenario)
|
84 |
+
with st.expander("Story"):
|
85 |
+
st.write(story)
|
86 |
+
|
87 |
+
st.audio("audio.mp3")
|
88 |
+
|
89 |
+
if __name__ == "__main__":
|
90 |
+
main()
|