Spaces:
Runtime error
Runtime error
GurpreetKJ
commited on
Commit
•
b5ce68a
1
Parent(s):
7754806
Uploaded Gen App.py
Browse files
app.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
from typing import Any
|
4 |
+
|
5 |
+
import requests
|
6 |
+
import streamlit as st
|
7 |
+
from dotenv import find_dotenv, load_dotenv
|
8 |
+
from langchain.chains import LLMChain
|
9 |
+
from langchain.chat_models import ChatOpenAI
|
10 |
+
from langchain.prompts import PromptTemplate
|
11 |
+
from transformers import pipeline
|
12 |
+
|
13 |
+
from utils.custom import css_code
|
14 |
+
|
15 |
+
load_dotenv(find_dotenv())
|
16 |
+
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
17 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
18 |
+
|
19 |
+
|
20 |
+
def progress_bar(amount_of_time: int) -> Any:
|
21 |
+
"""
|
22 |
+
A very simple progress bar the increases over time,
|
23 |
+
then disappears when it reached completion
|
24 |
+
:param amount_of_time: time taken
|
25 |
+
:return: None
|
26 |
+
"""
|
27 |
+
progress_text = "Please wait, Generative models hard at work"
|
28 |
+
my_bar = st.progress(0, text=progress_text)
|
29 |
+
|
30 |
+
for percent_complete in range(amount_of_time):
|
31 |
+
time.sleep(0.04)
|
32 |
+
my_bar.progress(percent_complete + 1, text=progress_text)
|
33 |
+
time.sleep(1)
|
34 |
+
my_bar.empty()
|
35 |
+
|
36 |
+
|
37 |
+
def generate_text_from_image(url: str) -> str:
|
38 |
+
"""
|
39 |
+
A function that uses the blip model to generate text from an image.
|
40 |
+
:param url: image location
|
41 |
+
:return: text: generated text from the image
|
42 |
+
"""
|
43 |
+
image_to_text: Any = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
44 |
+
|
45 |
+
generated_text: str = image_to_text(url)[0]["generated_text"]
|
46 |
+
|
47 |
+
print(f"IMAGE INPUT: {url}")
|
48 |
+
print(f"GENERATED TEXT OUTPUT: {generated_text}")
|
49 |
+
return generated_text
|
50 |
+
|
51 |
+
|
52 |
+
def generate_story_from_text(scenario: str) -> str:
|
53 |
+
"""
|
54 |
+
A function using a prompt template and GPT to generate a short story. LangChain is also
|
55 |
+
used for chaining purposes
|
56 |
+
:param scenario: generated text from the image
|
57 |
+
:return: generated story from the text
|
58 |
+
"""
|
59 |
+
prompt_template: str = f"""
|
60 |
+
You are a talented story teller who can create a story from a simple narrative./
|
61 |
+
Create a story using the following scenario; the story should have be maximum 50 words long;
|
62 |
+
|
63 |
+
CONTEXT: {scenario}
|
64 |
+
STORY:
|
65 |
+
"""
|
66 |
+
|
67 |
+
prompt: PromptTemplate = PromptTemplate(template=prompt_template, input_variables=["scenario"])
|
68 |
+
|
69 |
+
llm: Any = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.9)
|
70 |
+
|
71 |
+
story_llm: Any = LLMChain(llm=llm, prompt=prompt, verbose=True)
|
72 |
+
|
73 |
+
generated_story: str = story_llm.predict(scenario=scenario)
|
74 |
+
|
75 |
+
print(f"TEXT INPUT: {scenario}")
|
76 |
+
print(f"GENERATED STORY OUTPUT: {generated_story}")
|
77 |
+
return generated_story
|
78 |
+
|
79 |
+
|
80 |
+
def generate_speech_from_text(message: str) -> Any:
|
81 |
+
"""
|
82 |
+
A function using the ESPnet text to speech model from HuggingFace
|
83 |
+
:param message: short story generated by the GPT model
|
84 |
+
:return: generated audio from the short story
|
85 |
+
"""
|
86 |
+
API_URL: str = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
|
87 |
+
headers: dict[str, str] = {"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"}
|
88 |
+
payloads: dict[str, str] = {
|
89 |
+
"inputs": message
|
90 |
+
}
|
91 |
+
|
92 |
+
response: Any = requests.post(API_URL, headers=headers, json=payloads)
|
93 |
+
with open("generated_audio.flac", "wb") as file:
|
94 |
+
file.write(response.content)
|
95 |
+
|
96 |
+
|
97 |
+
def main() -> None:
|
98 |
+
"""
|
99 |
+
Main function
|
100 |
+
:return: None
|
101 |
+
"""
|
102 |
+
st.set_page_config(page_title= "IMAGE TO STORY CONVERTER", page_icon= "🖼️")
|
103 |
+
|
104 |
+
st.markdown(css_code, unsafe_allow_html=True)
|
105 |
+
|
106 |
+
with st.sidebar:
|
107 |
+
st.image("img/gkj.jpg")
|
108 |
+
st.write("---")
|
109 |
+
st.write("AI App created by @ Gurpreet Kaur")
|
110 |
+
|
111 |
+
st.header("Image-to-Story Converter")
|
112 |
+
uploaded_file: Any = st.file_uploader("Please choose a file to upload", type="jpg")
|
113 |
+
|
114 |
+
if uploaded_file is not None:
|
115 |
+
print(uploaded_file)
|
116 |
+
bytes_data: Any = uploaded_file.getvalue()
|
117 |
+
with open(uploaded_file.name, "wb") as file:
|
118 |
+
file.write(bytes_data)
|
119 |
+
st.image(uploaded_file, caption="Uploaded Image",
|
120 |
+
use_column_width=True)
|
121 |
+
progress_bar(100)
|
122 |
+
scenario: str = generate_text_from_image(uploaded_file.name)
|
123 |
+
story: str = generate_story_from_text(scenario)
|
124 |
+
generate_speech_from_text(story)
|
125 |
+
|
126 |
+
with st.expander("Generated Image scenario"):
|
127 |
+
st.write(scenario)
|
128 |
+
with st.expander("Generated short story"):
|
129 |
+
st.write(story)
|
130 |
+
|
131 |
+
st.audio("generated_audio.flac")
|
132 |
+
|
133 |
+
|
134 |
+
if __name__ == "__main__":
|
135 |
+
main()
|