|
|
|
import os |
|
import streamlit as st |
|
import requests |
|
from transformers import pipeline |
|
import openai |
|
|
|
|
|
import warnings |
|
warnings.filterwarnings("ignore") |
|
|
|
|
|
def img2txt(url): |
|
print("Initializing captioning model...") |
|
captioning_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") |
|
|
|
print("Generating text from the image...") |
|
text = captioning_model(url, max_new_tokens=20)[0]["generated_text"] |
|
|
|
print("Text generated successfully.") |
|
return text |
|
|
|
|
|
def txt2story(img_text): |
|
print("Initializing client...") |
|
client = openai.OpenAI( |
|
api_key=os.environ["TOGETHER_API_KEY"], |
|
base_url='https://api.together.xyz', |
|
) |
|
|
|
print("Constructing prompt for story generation...") |
|
content_prompt = f'''Based on the image description "{img_text}", conclude the story. |
|
Resolve the conflict or summarize the outcome of the situation. Ensure the story MUST |
|
have a definitive ending. The end.''' |
|
|
|
print("Preparing message sequences for interaction...") |
|
messages = [ |
|
{"role": "system", "content": "Once upon a time..."}, |
|
{"role": "user", "content": img_text, "temperature": 1}, |
|
{"role": "system", |
|
"content": content_prompt, |
|
"temperature": 0.7}, |
|
] |
|
|
|
print("Generating story completion using the AI model...") |
|
chat_completion = client.chat.completions.create( |
|
messages=messages, |
|
model="mistralai/Mixtral-8x7B-Instruct-v0.1", |
|
max_tokens=200) |
|
|
|
print("Story generated successfully.") |
|
return chat_completion.choices[0].message.content |
|
|
|
|
|
|
|
def txt2speech(text): |
|
print("Initializing text-to-speech conversion...") |
|
API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits" |
|
headers = {"Authorization": f"Bearer {os.environ['HUGGINGFACEHUB_API_TOKEN']}"} |
|
payloads = {'inputs': text} |
|
|
|
print("Sending request for speech synthesis...") |
|
response = requests.post(API_URL, headers=headers, json=payloads) |
|
|
|
print("Saving synthesized speech to audio file...") |
|
with open('audio_story.mp3', 'wb') as file: |
|
file.write(response.content) |
|
|
|
print("Text-to-speech conversion completed.") |
|
|
|
|
|
|
|
def main(): |
|
st.set_page_config(page_title="π¨ Image-to-Audio Story π§", page_icon="πΌοΈ") |
|
st.title("Turn the Image into Audio Story") |
|
|
|
|
|
uploaded_file = st.file_uploader("# π· Upload an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
bytes_data = uploaded_file.read() |
|
with open("uploaded_image.jpg", "wb") as file: |
|
file.write(bytes_data) |
|
|
|
st.image(uploaded_file, caption='πΌοΈ Uploaded Image', use_column_width=True) |
|
|
|
|
|
with st.spinner("## π€ AI is at Work! "): |
|
scenario = img2txt("uploaded_image.jpg") |
|
story = txt2story(scenario) |
|
txt2speech(story) |
|
|
|
st.markdown("---") |
|
st.markdown("## π Image Caption") |
|
st.write(scenario) |
|
|
|
st.markdown("---") |
|
st.markdown("## π Story") |
|
st.write(story) |
|
|
|
st.markdown("---") |
|
st.markdown("## π§ Audio Story") |
|
st.audio("audio_story.mp3") |
|
|
|
if __name__ == '__main__': |
|
main() |
|
|
|
|
|
st.markdown("### Credits") |
|
st.caption(''' |
|
Made with β€οΈ by @Aditya-Neural-Net-Ninja\n |
|
Utilizes Image-to-text, Text Generation, Text-to-speech Transformer Models\n |
|
Gratitude to Streamlit, π€ Spaces for Deployment & Hosting |
|
''') |