File size: 2,497 Bytes
33903b2
 
 
 
 
 
 
3fce1a1
 
401f179
33903b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401f179
33903b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15f27fa
b8dff16
3fce1a1
b8dff16
33903b2
 
3145e0e
33903b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34a52d9
33903b2
15f27fa
33903b2
 
 
 
 
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
from transformers import pipeline
from dotenv import find_dotenv, load_dotenv
from langchain import PromptTemplate, LLMChain, HuggingFaceHub
import streamlit as st
import requests
import os


load_dotenv(find_dotenv())
huggingface_api_key = os.getenv("HUGGINGFACE_API")


def image2text(url):
    image_to_text = pipeline('image-to-text', model='Salesforce/blip-image-captioning-large')
    text = image_to_text(url)[0]['generated_text']
    print(text)
    return text



def generate_story(scenario, length):
  template = """
  You are  story teller, generate a short story in {length} words\n
  CONTEXT:{scenario}\n
  STORY:
  """

  prompt = PromptTemplate(template=template, input_variables=["scenario","length"])
  llm = LLMChain(llm=HuggingFaceHub(huggingfacehub_api_token=huggingface_api_key, repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1"), prompt=prompt, verbose=True)
  story = llm.predict(scenario=scenario, length=length)
  print(story)
  return story



# def text2speech(message):
#     API_URL = "https://api-inference.huggingface.co/models/microsoft/speecht5_tts"
#     headers = {"Authorization": f"Bearer {HUGGINGFACE_API}"}
#     payloads = {
#         "inputs": message
#     }
#     response = requests.post(API_URL,headers=headers,json=payloads)
#     with open('audio.wav', 'wb') as file:
#       file.write(response.content)



def main():
    st.set_page_config(page_title="Image Storyteller")

    st.header("Image to Story")
    length = st.number_input("Length")
    if not length:
        length = 10
    uploaded_file = st.file_uploader("Choose an Image", type="jpg")

    
    scenario = "" 
    successful_processing = False  

    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.name, caption="Uploaded Image", use_column_width=True)

        try:
            scenario = image2text(uploaded_file.name)
            successful_processing = True
        except Exception as e:
            st.error(f"Error processing the image: {e}")

        if successful_processing:
            story = generate_story(scenario, length)
            # text2speech(story)

            with st.expander("Scenario"):
                st.write(scenario)
            with st.expander("Generated Story"):
                st.write(story)
            # st.audio('audio.wav')

if __name__ == '__main__':
    main()