iohanngrig commited on
Commit
1793f90
·
verified ·
1 Parent(s): 23e5d9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -19
app.py CHANGED
@@ -8,12 +8,22 @@ from langchain.chains import LLMChain
8
  from langchain.chat_models import ChatOpenAI
9
  from langchain.prompts import PromptTemplate
10
  from transformers import pipeline
11
- from utils import css_code
12
 
13
  HUGGINGFACE_API_TOKEN = st.secrets["HUGGINGFACE_API_TOKEN"]
14
  OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
15
  MODEL = st.secrets["MODEL2"]
16
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def progress_bar(amount_of_time: int) -> Any:
19
  """
@@ -31,7 +41,6 @@ def progress_bar(amount_of_time: int) -> Any:
31
  time.sleep(1)
32
  my_bar.empty()
33
 
34
-
35
  def generate_text_from_image(url: str) -> str:
36
  """
37
  A function that uses the blip model to generate text from an image.
@@ -39,14 +48,11 @@ def generate_text_from_image(url: str) -> str:
39
  :return: text: generated text from the image
40
  """
41
  image_to_text: Any = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
42
-
43
  generated_text: str = image_to_text(url)[0]["generated_text"]
44
-
45
  print(f"IMAGE INPUT: {url}")
46
  print(f"GENERATED TEXT OUTPUT: {generated_text}")
47
  return generated_text
48
 
49
-
50
  def generate_story_from_text(scenario: str) -> str:
51
  """
52
  A function using a prompt template and GPT to generate a short story. LangChain is also
@@ -57,24 +63,18 @@ def generate_story_from_text(scenario: str) -> str:
57
  prompt_template: str = f"""
58
  You are a story teller;
59
  You can generate a long story based on a simple narrative, the story should be no more than 100 words and have more than 30 words;
60
-
61
  CONTEXT: {scenario}
62
  STORY:
63
  """
64
-
65
  prompt: PromptTemplate = PromptTemplate(template=prompt_template, input_variables=["scenario"])
66
-
67
  llm: Any = ChatOpenAI(model_name=MODEL, temperature=1)
68
-
69
  story_llm: Any = LLMChain(llm=llm, prompt=prompt, verbose=True)
70
-
71
  generated_story: str = story_llm.predict(scenario=scenario)
72
-
73
  print(f"TEXT INPUT: {scenario}")
74
  print(f"GENERATED STORY OUTPUT: {generated_story}")
75
  return generated_story
76
 
77
-
78
  def generate_speech_from_text(message: str) -> Any:
79
  """
80
  A function using the ESPnet text to speech model from HuggingFace
@@ -96,16 +96,13 @@ def generate_speech_from_text(message: str) -> Any:
96
  mime='flac',
97
  )
98
 
99
-
100
  def main() -> None:
101
  """
102
  Main function
103
  :return: None
104
  """
105
  st.set_page_config(page_title="Image to audio story", page_icon="img/logo.png", layout="wide")
106
-
107
  st.markdown(css_code, unsafe_allow_html=True)
108
-
109
  with st.sidebar:
110
  st.image("img/kandinsky.jpg")
111
  #st.write("---")
@@ -124,14 +121,12 @@ def main() -> None:
124
  progress_bar(100)
125
  scenario: str = generate_text_from_image(uploaded_file.name)
126
  story: str = generate_story_from_text(scenario)
127
- #generate_speech_from_text(story)
128
-
129
  with st.expander("Generated scenario"):
130
  st.write(scenario)
131
  with st.expander("Generated story"):
132
  st.write(story)
133
-
134
- #st.audio("generated_audio.flac")
135
 
136
 
137
  if __name__ == "__main__":
 
8
  from langchain.chat_models import ChatOpenAI
9
  from langchain.prompts import PromptTemplate
10
  from transformers import pipeline
 
11
 
12
  HUGGINGFACE_API_TOKEN = st.secrets["HUGGINGFACE_API_TOKEN"]
13
  OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
14
  MODEL = st.secrets["MODEL2"]
15
 
16
+ css_code = """
17
+ <style>
18
+ section[data-testid="stSidebar"] > div > div:nth-child(2) {
19
+ padding-top: 0.75rem !important;
20
+ }
21
+
22
+ section.main > div {
23
+ padding-top: 64px;
24
+ }
25
+ </style>
26
+ """
27
 
28
  def progress_bar(amount_of_time: int) -> Any:
29
  """
 
41
  time.sleep(1)
42
  my_bar.empty()
43
 
 
44
  def generate_text_from_image(url: str) -> str:
45
  """
46
  A function that uses the blip model to generate text from an image.
 
48
  :return: text: generated text from the image
49
  """
50
  image_to_text: Any = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
 
51
  generated_text: str = image_to_text(url)[0]["generated_text"]
 
52
  print(f"IMAGE INPUT: {url}")
53
  print(f"GENERATED TEXT OUTPUT: {generated_text}")
54
  return generated_text
55
 
 
56
  def generate_story_from_text(scenario: str) -> str:
57
  """
58
  A function using a prompt template and GPT to generate a short story. LangChain is also
 
63
  prompt_template: str = f"""
64
  You are a story teller;
65
  You can generate a long story based on a simple narrative, the story should be no more than 100 words and have more than 30 words;
66
+
67
  CONTEXT: {scenario}
68
  STORY:
69
  """
 
70
  prompt: PromptTemplate = PromptTemplate(template=prompt_template, input_variables=["scenario"])
 
71
  llm: Any = ChatOpenAI(model_name=MODEL, temperature=1)
 
72
  story_llm: Any = LLMChain(llm=llm, prompt=prompt, verbose=True)
 
73
  generated_story: str = story_llm.predict(scenario=scenario)
 
74
  print(f"TEXT INPUT: {scenario}")
75
  print(f"GENERATED STORY OUTPUT: {generated_story}")
76
  return generated_story
77
 
 
78
  def generate_speech_from_text(message: str) -> Any:
79
  """
80
  A function using the ESPnet text to speech model from HuggingFace
 
96
  mime='flac',
97
  )
98
 
 
99
  def main() -> None:
100
  """
101
  Main function
102
  :return: None
103
  """
104
  st.set_page_config(page_title="Image to audio story", page_icon="img/logo.png", layout="wide")
 
105
  st.markdown(css_code, unsafe_allow_html=True)
 
106
  with st.sidebar:
107
  st.image("img/kandinsky.jpg")
108
  #st.write("---")
 
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
  with st.expander("Generated scenario"):
126
  st.write(scenario)
127
  with st.expander("Generated story"):
128
  st.write(story)
129
+ st.audio("generated_audio.flac")
 
130
 
131
 
132
  if __name__ == "__main__":