Spaces:
Sleeping
Sleeping
File size: 4,793 Bytes
ee99dd0 068d689 268f3a0 d2898bf 268f3a0 068d689 268f3a0 b00bfb2 268f3a0 b00bfb2 268f3a0 068d689 54751e4 7915f9c 54751e4 7915f9c 54751e4 7915f9c 54751e4 19a195c 54751e4 38224de 54751e4 0a7a178 068d689 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
import os
import streamlit as st
import requests
from transformers import pipeline
import openai
from langchain import LLMChain, PromptTemplate
from langchain import HuggingFaceHub
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
import torch
from diffusers import DiffusionPipeline
# Suppressing all warnings
import warnings
warnings.filterwarnings("ignore")
api_token = os.getenv('H_TOKEN')
# Image-to-text
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)
return text
# Text-to-story
model = "tiiuae/falcon-7b-instruct"
llm = HuggingFaceHub(
huggingfacehub_api_token = api_token,
repo_id = model,
verbose = False,
model_kwargs = {"temperature":0.2, "max_new_tokens": 4000})
def generate_story(scenario, llm):
template= """You are a story teller.
You get a scenario as an input text, and generates a short story out of it.
Context: {scenario}
Story:
"""
prompt = PromptTemplate(template=template, input_variables=["scenario"])
#Let's create our LLM chain now
chain = LLMChain(prompt=prompt, llm=llm)
story = chain.predict(scenario=scenario)
start_index = story.find("Story:") + len("Story:")
# Extract the text after "Story:"
story = story[start_index:].strip()
return story
# Text-to-speech
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 {api_token }"}
payloads = {'inputs': text}
response = requests.post(API_URL, headers=headers, json=payloads)
with open('audio_story.mp3', 'wb') as file:
file.write(response.content)
# text-to- image
def txt2img(text, style="realistic"):
model_id = "stabilityai/stable-diffusion-2"
#pipeline = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
# Use the Euler scheduler here instead
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
image = pipe(prompt = text, guidance_scale = 7.5).images[0]
return image
st.sidebar.title("Choose the task")
# Function for the Audio Story page
def audio_story_page():
st.title("π¨ Image-to-Audio Story π§")
st.write("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 = generate_story(scenario, llm)
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")
# Function for the Image Generator page
def image_generator_page():
st.title("Stable Diffusion Image Generation")
st.write("This app lets you generate images using Stable Diffusion with the Euler scheduler.")
prompt = st.text_input("Enter your prompt:")
image_style = st.selectbox("Style Selection", ["realistic", "cartoon", "watercolor"])
if st.button("Generate Image"):
if prompt:
with st.spinner("Generating image..."):
image = txt2img(prompt, style=image_style)
st.image(image)
else:
st.error("Please enter a prompt...")
# Function for the Home page
def home_page():
st.title("Welcome to your Creative Canvas!")
st.write("Use the tools in the sidebar to create audio stories and unique images.")
# Streamlit web app main function
def main():
selection = st.sidebar.radio("Go to", ["Home", "Audio Story", "Image Generator"])
if selection == "Home":
home_page()
elif selection == "Audio Story":
audio_story_page()
elif selection == "Image Generator":
image_generator_page()
if __name__ == '__main__':
main() |