Spaces:
Configuration error
Configuration error
File size: 1,740 Bytes
ce8fc87 |
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 |
import gradio as gr
import dotenv
from transformers import pipeline
from langchain import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
import requests
import os
dotenv.load_dotenv()
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
# image to text
def imgToText(url):
img_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
text = img_to_text(url)[0]['generated_text']
return text
# LLM
def generate_story(scenario):
template = """
You are a story teller.
You can generate a short story based on a simple narrative, the story should be no more than 40 words:
CONTEXT: {scenario}
STORY:
"""
prompt = PromptTemplate(template=template, input_variables=["scenario"])
story_llm = LLMChain(llm=OpenAI(model_name="gpt-3.5-turbo"), prompt=prompt, verbose=True)
story = story_llm.predict(scenario=scenario)
return story
def textToSpeech(story):
API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
headers = {"Authorization": "Bearer " + HUGGINGFACEHUB_API_TOKEN}
payload = {"inputs": story}
response = requests.post(API_URL, headers=headers, json=payload)
with open("story.flac", "wb") as f:
f.write(response.content)
def generate_story_and_play_audio(image):
scenario = imgToText(image.name)
story = generate_story(scenario)
textToSpeech(story)
return "story.flac"
iface = gr.Interface(
fn=generate_story_and_play_audio,
inputs=gr.inputs.File(label="Upload an image"),
outputs=gr.outputs.Audio(label="Generated Story", type="filepath")
)
iface.launch()
|