Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 generated successfully.")
|
21 |
+
return text
|
22 |
+
|
23 |
+
# Text-to-story
|
24 |
+
def txt2story(img_text):
|
25 |
+
print("Initializing client...")
|
26 |
+
client = openai.OpenAI(
|
27 |
+
api_key=os.environ["TOGETHER_API_KEY"],
|
28 |
+
base_url='https://api.together.xyz',
|
29 |
+
)
|
30 |
+
|
31 |
+
print("Constructing prompt for story generation...")
|
32 |
+
content_prompt = f'''Based on the image description "{img_text}", conclude the story.
|
33 |
+
Resolve the conflict or summarize the outcome of the situation. Ensure the story MUST
|
34 |
+
have a definitive ending. The end.'''
|
35 |
+
|
36 |
+
print("Preparing message sequences for interaction...")
|
37 |
+
messages = [
|
38 |
+
{"role": "system", "content": "Once upon a time..."},
|
39 |
+
{"role": "user", "content": img_text, "temperature": 1},
|
40 |
+
{"role": "system",
|
41 |
+
"content": content_prompt,
|
42 |
+
"temperature": 0.7},
|
43 |
+
]
|
44 |
+
|
45 |
+
print("Generating story completion using the AI model...")
|
46 |
+
chat_completion = client.chat.completions.create(
|
47 |
+
messages=messages,
|
48 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
49 |
+
max_tokens=200)
|
50 |
+
|
51 |
+
print("Story generated successfully.")
|
52 |
+
return chat_completion.choices[0].message.content
|
53 |
+
|
54 |
+
|
55 |
+
# Text-to-speech
|
56 |
+
def txt2speech(text):
|
57 |
+
print("Initializing text-to-speech conversion...")
|
58 |
+
API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
|
59 |
+
headers = {"Authorization": f"Bearer {os.environ['HUGGINGFACEHUB_API_TOKEN']}"}
|
60 |
+
payloads = {'inputs': text}
|
61 |
+
|
62 |
+
print("Sending request for speech synthesis...")
|
63 |
+
response = requests.post(API_URL, headers=headers, json=payloads)
|
64 |
+
|
65 |
+
print("Saving synthesized speech to audio file...")
|
66 |
+
with open('audio_story.mp3', 'wb') as file:
|
67 |
+
file.write(response.content)
|
68 |
+
|
69 |
+
print("Text-to-speech conversion completed.")
|
70 |
+
|
71 |
+
|
72 |
+
# Streamlit web app main function
|
73 |
+
def main():
|
74 |
+
st.set_page_config(page_title="π¨ Image-to-Audio Story π§", page_icon="πΌοΈ")
|
75 |
+
st.title("Turn the Image into Audio Story")
|
76 |
+
|
77 |
+
# Allows users to upload an image file
|
78 |
+
uploaded_file = st.file_uploader("# π· Upload an image...", type=["jpg", "jpeg", "png"])
|
79 |
+
|
80 |
+
if uploaded_file is not None:
|
81 |
+
# Reads and saves uploaded image file
|
82 |
+
bytes_data = uploaded_file.read()
|
83 |
+
with open("uploaded_image.jpg", "wb") as file:
|
84 |
+
file.write(bytes_data)
|
85 |
+
|
86 |
+
st.image(uploaded_file, caption='πΌοΈ Uploaded Image', use_column_width=True)
|
87 |
+
|
88 |
+
# Initiates AI processing and story generation
|
89 |
+
with st.spinner("## π€ AI is at Work! "):
|
90 |
+
scenario = img2txt("uploaded_image.jpg") # Extracts text from the image
|
91 |
+
story = txt2story(scenario) # Generates a story based on the image text
|
92 |
+
txt2speech(story) # Converts the story to audio
|
93 |
+
|
94 |
+
st.markdown("---")
|
95 |
+
st.markdown("## π Image Caption")
|
96 |
+
st.write(scenario)
|
97 |
+
|
98 |
+
st.markdown("---")
|
99 |
+
st.markdown("## π Story")
|
100 |
+
st.write(story)
|
101 |
+
|
102 |
+
st.markdown("---")
|
103 |
+
st.markdown("## π§ Audio Story")
|
104 |
+
st.audio("audio_story.mp3")
|
105 |
+
|
106 |
+
if __name__ == '__main__':
|
107 |
+
main()
|
108 |
+
|
109 |
+
# Credits
|
110 |
+
st.markdown("### Credits")
|
111 |
+
st.caption('''
|
112 |
+
Made with β€οΈ by @Aditya-Neural-Net-Ninja\n
|
113 |
+
Utilizes Image-to-text, Text Generation, Text-to-speech Transformer Models\n
|
114 |
+
Gratitude to Streamlit, π€ Spaces for Deployment & Hosting
|
115 |
+
''')
|