Spaces:
Runtime error
Runtime error
Added Files
Browse filesAdded working files
- ImageToStory.py +76 -0
- requirements.txt +5 -0
ImageToStory.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
from dotenv import find_dotenv, load_dotenv
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
from langchain import PromptTemplate, LLMChain
|
| 9 |
+
from langchain.llms import GooglePalm
|
| 10 |
+
|
| 11 |
+
load_dotenv(find_dotenv())
|
| 12 |
+
|
| 13 |
+
llm = GooglePalm(temperature=0.9, google_api_key=os.getenv("GOOGLE_API_KEY"))
|
| 14 |
+
|
| 15 |
+
# Iamge to Text
|
| 16 |
+
def image_to_text(url):
|
| 17 |
+
#load a transformer
|
| 18 |
+
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 19 |
+
|
| 20 |
+
text = image_to_text(url)[0]['generated_text']
|
| 21 |
+
|
| 22 |
+
print (text)
|
| 23 |
+
return text
|
| 24 |
+
|
| 25 |
+
# llm
|
| 26 |
+
def generate_story(scenario):
|
| 27 |
+
template = """
|
| 28 |
+
you are a very good story teller and a very rude person:
|
| 29 |
+
you can generate a short fairy tail based on a single narrative, the story should take 5 seconds to read.
|
| 30 |
+
|
| 31 |
+
CONTEXT: {scenario}
|
| 32 |
+
STORY:
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
prompt = PromptTemplate(template=template, input_variables=["scenario"])
|
| 36 |
+
story_llm = LLMChain(llm=llm, prompt=prompt, verbose=True)
|
| 37 |
+
story = story_llm.predict(scenario=scenario)
|
| 38 |
+
print(story)
|
| 39 |
+
return story
|
| 40 |
+
|
| 41 |
+
# text to speech
|
| 42 |
+
|
| 43 |
+
def text_to_speech(message):
|
| 44 |
+
API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
|
| 45 |
+
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_TOKEN')}"}
|
| 46 |
+
payload = {"inputs": message}
|
| 47 |
+
|
| 48 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 49 |
+
print(response.content)
|
| 50 |
+
with open('audio.mp3', 'wb') as audio_file:
|
| 51 |
+
audio_file.write(response.content)
|
| 52 |
+
|
| 53 |
+
def main():
|
| 54 |
+
st.set_page_config(page_title="Image to Story", page_icon="📚", layout="wide")
|
| 55 |
+
|
| 56 |
+
st.title("Image to Story")
|
| 57 |
+
uploaded_file = st.file_uploader("Choose an image...", type="png")
|
| 58 |
+
|
| 59 |
+
if uploaded_file is not None:
|
| 60 |
+
bytes_data = uploaded_file.getvalue()
|
| 61 |
+
with open(uploaded_file.name, "wb") as file:
|
| 62 |
+
file.write(bytes_data)
|
| 63 |
+
st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True)
|
| 64 |
+
scenario = image_to_text(uploaded_file.name)
|
| 65 |
+
story = generate_story(scenario)
|
| 66 |
+
text_to_speech(story)
|
| 67 |
+
|
| 68 |
+
with st.expander("scenerio"):
|
| 69 |
+
st.write(scenario)
|
| 70 |
+
with st.expander("story"):
|
| 71 |
+
st.write(story)
|
| 72 |
+
|
| 73 |
+
st.audio("audio.mp3")
|
| 74 |
+
|
| 75 |
+
if __name__ == '__main__':
|
| 76 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
google-generativeai
|
| 2 |
+
langchain
|
| 3 |
+
python-dotenv
|
| 4 |
+
tensorflow
|
| 5 |
+
transformers
|