Spaces:
Sleeping
Sleeping
from dotenv import find_dotenv, load_dotenv | |
from transformers import pipeline | |
from langchain import PromptTemplate, LLMChain, OpenAI | |
import requests | |
import os | |
import streamlit as st | |
import json | |
load_dotenv (find_dotenv()) | |
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN") | |
#img2text | |
def img2text(url): | |
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") | |
text = image_to_text(url)[0]["generated_text"] | |
print(text) | |
return text | |
#llm | |
def generate_story(scenario): | |
template = """ | |
Vous êtes un conteur ; | |
Vous pouvez créer une histoire courte à partir d'un récit simple. L'histoire ne doit pas dépasser 50 mots; | |
CONTEXT: {scenario} | |
STORY: | |
""" | |
prompt = PromptTemplate(template=template, input_variables=["scenario"]) | |
story_llm = LLMChain(llm=OpenAI( | |
model_name="gpt-3.5-turbo", temperature=1), prompt=prompt, verbose=True) | |
story = story_llm.predict(scenario=scenario) | |
print(story) | |
return story | |
#text to speech | |
def text2speech(message): | |
#API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits" | |
API_URL = "https://api-inference.huggingface.co/models/facebook/mms-tts-fra" | |
headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"} | |
payloads = { | |
"inputs": message | |
} | |
response = requests.post(API_URL, headers=headers, json=payloads) | |
with open('audio.wav', 'wb') as file: | |
file.write(response.content) | |
#translate | |
def translatefr(Text2img2text): | |
API_URL = "https://api-inference.huggingface.co/models/sgugger/marian-finetuned-kde4-en-to-fr" | |
headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"} | |
payloads = { | |
"inputs": Text2img2text | |
} | |
response = requests.post(API_URL, headers=headers, json=payloads) | |
responsejson = response.content | |
# Votre donnée JSON encodée en bytes | |
data_bytes = responsejson | |
# Décoder les bytes en string | |
data_str = data_bytes.decode('utf-8') | |
# Analyser la chaîne JSON pour obtenir un objet Python | |
data = json.loads(data_str) | |
# Extraire le texte souhaité | |
text = data[0]['translation_text'] | |
print(text) | |
return text | |
#scenario = img2text("mmd.png") | |
#story = generate_story(scenario) | |
#en_fr_translator = pipeline("translation_en_to_fr") | |
#story_fr = en_fr_translator(story)[0]["translation_text"] | |
#print(story_fr) | |
#text2speech(story_fr) | |
def main(): | |
st.set_page_config(page_title="Img 2 audio story") | |
st.header("Turn img into audio story") | |
uploaded_file = st.file_uploader("Choose an image....", type="jpg") | |
if uploaded_file is not None: | |
print(uploaded_file) | |
bytes_data = uploaded_file.getvalue() | |
with open(uploaded_file.name, "wb") as file: | |
file.write(bytes_data) | |
st.image(uploaded_file, caption='Uploaded Image.', | |
use_column_width=True) | |
scenario = img2text(uploaded_file.name) | |
scenariofr = translatefr(scenario) | |
story = generate_story(scenariofr) | |
#en_fr_translator = pipeline("translation_en_to_fr") | |
#story_fr = en_fr_translator(story)[0]["translation_text"] | |
text2speech(story) | |
with st.expander("scenario"): | |
st.write(scenariofr) | |
with st.expander("story"): | |
st.write(story) | |
st.audio("audio.wav") | |
if __name__ == '__main__': | |
main() | |