app file added
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import find_dotenv, load_dotenv
|
2 |
+
from transformers import pipeline
|
3 |
+
from langchain import PromptTemplate, LLMChain, OpenAI
|
4 |
+
import requests
|
5 |
+
import os
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
+
load_dotenv(find_dotenv())
|
9 |
+
HF_API_KEY=os.getenv("HF_API_KEY")
|
10 |
+
|
11 |
+
# img2text
|
12 |
+
def img2text(url):
|
13 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
|
14 |
+
text = image_to_text_model(url)[0]["generated_text"]
|
15 |
+
|
16 |
+
print(text)
|
17 |
+
return text
|
18 |
+
|
19 |
+
|
20 |
+
# Describe it using LLM
|
21 |
+
def generate_description(caption):
|
22 |
+
template = """
|
23 |
+
You are a story teller;
|
24 |
+
You can generate a short story based on a simple narrative, the story should be no more than 30 words;
|
25 |
+
CONTEXT: {caption}
|
26 |
+
STORY;
|
27 |
+
"""
|
28 |
+
|
29 |
+
prompt = PromptTemplate(template=template, input_variables=["caption"])
|
30 |
+
|
31 |
+
desc_llm = LLMChain(llm=OpenAI(model_name="gpt-4", temperature=1), prompt=prompt, verbose=True)
|
32 |
+
description = desc_llm.predict(caption=caption).replace('"', '')
|
33 |
+
|
34 |
+
print(description)
|
35 |
+
return description
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
# text to speech
|
40 |
+
def text2speech(message):
|
41 |
+
API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
|
42 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
43 |
+
payload = {
|
44 |
+
"inputs": message
|
45 |
+
}
|
46 |
+
|
47 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
48 |
+
with open('audio.flac', 'wb') as file:
|
49 |
+
file.write(response.content)
|
50 |
+
|
51 |
+
|
52 |
+
def main():
|
53 |
+
st.set_page_config(page_title="image-to-caption-to-summary", page_icon="😊")
|
54 |
+
st.header("Image to caption to summary")
|
55 |
+
uploaded_file = st.file_uploader("Choose an image", type=['png', 'jpg'])
|
56 |
+
|
57 |
+
if uploaded_file is not None:
|
58 |
+
print(uploaded_file)
|
59 |
+
bytes_data = uploaded_file.getvalue()
|
60 |
+
with open(uploaded_file.name, "wb") as file:
|
61 |
+
file.write(bytes_data)
|
62 |
+
|
63 |
+
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
64 |
+
|
65 |
+
st.text('Processing img2text...')
|
66 |
+
caption = img2text(uploaded_file.name)
|
67 |
+
with st.expander("caption"):
|
68 |
+
st.write(caption)
|
69 |
+
|
70 |
+
st.text('Generating description of given image...')
|
71 |
+
description = generate_description(caption)
|
72 |
+
with st.expander("Description"):
|
73 |
+
st.write(story)
|
74 |
+
|
75 |
+
st.text('Processing text2speech...')
|
76 |
+
text2speech(story)
|
77 |
+
st.audio("audio.flac")
|
78 |
+
|
79 |
+
if __name__ == '__main__':
|
80 |
+
main()
|