Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Imports
|
2 |
+
import os
|
3 |
+
import streamlit as st
|
4 |
+
import requests
|
5 |
+
from transformers import pipeline
|
6 |
+
import openai
|
7 |
+
|
8 |
+
# Suppressing all warnings
|
9 |
+
import warnings
|
10 |
+
warnings.filterwarnings("ignore")
|
11 |
+
|
12 |
+
# Image-to-text
|
13 |
+
def img2txt(url):
|
14 |
+
print("Initializing captioning model...")
|
15 |
+
captioning_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
16 |
+
|
17 |
+
print("Generating text from the image...")
|
18 |
+
text = captioning_model(url, max_new_tokens=20)[0]["generated_text"]
|
19 |
+
|
20 |
+
print(text)
|
21 |
+
return text
|
22 |
+
|
23 |
+
# Text-to-story
|
24 |
+
def txt2story(img_text, top_k, top_p, temperature):
|
25 |
+
|
26 |
+
headers = {"Authorization": f"Bearer {os.environ['TOGETHER_API_KEY']}"}
|
27 |
+
|
28 |
+
data = {
|
29 |
+
"model": "togethercomputer/llama-2-70b-chat",
|
30 |
+
"messages": [
|
31 |
+
{"role": "system", "content": '''As an experienced short story writer, write story title and then create a meaningful story influenced by provided words.
|
32 |
+
Ensure stories conclude positively within 100 words. Remember the story must end within 100 words''', "temperature": temperature},
|
33 |
+
{"role": "user", "content": f"Here is input set of words: {img_text}", "temperature": temperature}
|
34 |
+
],
|
35 |
+
"top_k": top_k,
|
36 |
+
"top_p": top_p,
|
37 |
+
"temperature": temperature
|
38 |
+
}
|
39 |
+
|
40 |
+
response = requests.post("https://api.together.xyz/inference", headers=headers, json=data)
|
41 |
+
|
42 |
+
story = response.json()["output"]["choices"][0]["text"]
|
43 |
+
return story
|
44 |
+
|
45 |
+
|
46 |
+
# Text-to-speech
|
47 |
+
def txt2speech(text):
|
48 |
+
print("Initializing text-to-speech conversion...")
|
49 |
+
API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
|
50 |
+
headers = {"Authorization": f"Bearer {os.environ['HUGGINGFACEHUB_API_TOKEN']}"}
|
51 |
+
payloads = {'inputs': text}
|
52 |
+
|
53 |
+
response = requests.post(API_URL, headers=headers, json=payloads)
|
54 |
+
|
55 |
+
with open('audio_story.mp3', 'wb') as file:
|
56 |
+
file.write(response.content)
|
57 |
+
|
58 |
+
|
59 |
+
# Streamlit web app main function
|
60 |
+
def main():
|
61 |
+
st.set_page_config(page_title="π¨ Image-to-Audio/Text Story π§", page_icon="πΌοΈ")
|
62 |
+
st.title("Turn the Image into Audio/Text Story")
|
63 |
+
|
64 |
+
# Allows users to upload an image file
|
65 |
+
uploaded_file = st.file_uploader("# π· Upload an image...", type=["jpg", "jpeg", "png"])
|
66 |
+
|
67 |
+
# Parameters for LLM model (in the sidebar)
|
68 |
+
st.sidebar.markdown("# LLM Inference Configuration Parameters")
|
69 |
+
top_k = st.sidebar.number_input("Top-K", min_value=1, max_value=100, value=5)
|
70 |
+
top_p = st.sidebar.number_input("Top-P", min_value=0.0, max_value=1.0, value=0.8)
|
71 |
+
temperature = st.sidebar.number_input("Temperature", min_value=0.1, max_value=2.0, value=1.5)
|
72 |
+
|
73 |
+
if uploaded_file is not None:
|
74 |
+
# Reads and saves uploaded image file
|
75 |
+
bytes_data = uploaded_file.read()
|
76 |
+
with open("uploaded_image.jpg", "wb") as file:
|
77 |
+
file.write(bytes_data)
|
78 |
+
|
79 |
+
st.image(uploaded_file, caption='πΌοΈ Uploaded Image', use_column_width=True)
|
80 |
+
|
81 |
+
# Initiates AI processing and story generation
|
82 |
+
with st.spinner("## π€ AI is at Work! "):
|
83 |
+
scenario = img2txt("uploaded_image.jpg") # Extracts text from the image
|
84 |
+
story = txt2story(scenario, top_k, top_p, temperature) # Generates a story based on the image text, LLM params
|
85 |
+
txt2speech(story) # Converts the story to audio
|
86 |
+
|
87 |
+
st.markdown("---")
|
88 |
+
st.markdown("## π Image Caption")
|
89 |
+
st.write(scenario)
|
90 |
+
|
91 |
+
st.markdown("---")
|
92 |
+
st.markdown("## π Story")
|
93 |
+
st.write(story)
|
94 |
+
|
95 |
+
st.markdown("---")
|
96 |
+
st.markdown("## π§ Audio Story")
|
97 |
+
st.audio("audio_story.mp3")
|
98 |
+
|
99 |
+
if __name__ == '__main__':
|
100 |
+
main()
|
101 |
+
|
102 |
+
# Credits
|
103 |
+
st.markdown("### Credits")
|
104 |
+
st.caption('''
|
105 |
+
Made by Nithin John\n
|
106 |
+
Utilizes Image-to-text, Text Generation, Text-to-speech Transformer Models\n
|
107 |
+
Gratitude to Streamlit, π€ Spaces for Deployment & Hosting
|
108 |
+
''')
|