-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
155 lines (112 loc) · 4.28 KB
/
app.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
from dotenv import find_dotenv, load_dotenv
import openai
import requests
import streamlit as st
from transformers import pipeline
USE_OPENAI_API = True
load_dotenv(find_dotenv())
HUGGINGFACE_HUB_API_TOKEN = os.environ.get("HUGGINGFACE_HUB_API_TOKEN")
if USE_OPENAI_API:
openai.api_key = os.environ.get("OPENAI_API_KEY")
def image_to_text(image_url: str) -> str:
# More details about tasks supported by the "transformers" library can be found here:
# https://huggingface.co/tasks
# Also:
# https://huggingface.co/tasks/image-to-text
pipe = pipeline("image-to-text",
# model="nlpconnect/vit-gpt2-image-captioning")
model="Salesforce/blip-image-captioning-large")
model_response = pipe(image_url)
print("image-to-text response:", model_response)
text_on_image = model_response[0]["generated_text"]
print("generated text:", text_on_image)
return text_on_image
def generate_story_openai(scenario: str) -> str:
prompt_template = """
CONTEXT: {scenario}
STORY:
"""
gpt_messages = [
{
"role": "system",
"content": """
You are a story teller. You can tell a short story based on a simple narrative.
The story should be no longer than 25 words and a few sentences.
""",
},
{
"role": "user",
"content": prompt_template.format(scenario=scenario),
},
]
print("generate_story_openai: Starting API request...")
api_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=gpt_messages,
)
print("generate_story_openai: OpenAI API response:", api_response)
response_message = api_response["choices"][0]["message"]
return response_message["content"]
def generate_story_oss_model(scenario: str) -> str:
prompt_template = """
You are a story teller.
You can tell a short story based on a simple narrative.
The story should be no longer than 25 words and a few sentences.
CONTEXT: {scenario}
STORY:
"""
pipe = pipeline("text-generation", model="TheBloke/Llama-2-70B-GPTQ") # Needs GPU :(
model_response = pipe(prompt_template.format(scenario=scenario))
# print("text-generation response:", model_response)
return "TODO: Implement integration (wasn't trivial to pick a model)"
def generate_story(scenario: str) -> str:
story = None
if USE_OPENAI_API:
story = generate_story_openai(scenario)
else:
story = generate_story_oss_model(scenario)
return story
def generate_speech(text: str, output_name="audio.flac") -> str:
API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
headers = {
"Authorization": f"Bearer {HUGGINGFACE_HUB_API_TOKEN}",
}
payload = {
"inputs": text,
}
print("generate_speech: Starting API request...")
response = requests.post(API_URL, headers=headers, json=payload)
with open(output_name, "wb") as f:
f.write(response.content)
return output_name
# def old_main():
# text_on_image = image_to_text("./test_images/Brick_sign_large__compressed.png")
# if USE_OPENAI_API:
# story = generate_story_openai(text_on_image)
# else:
# story = generate_story_oss_model(text_on_image)
# print("Story:", story)
# generate_speech(story)
def main():
st.set_page_config(page_title="Storyteller", page_icon="📖")
st.header("Turn any image into a compelling audio story")
uploaded_file = st.file_uploader(
"Choose an image...", type=["png", "jpg", "jpeg"])
if uploaded_file is not None:
print("Uploaded file:", uploaded_file)
bytes_data = uploaded_file.getvalue()
with open(uploaded_file.name, "wb") as image_file:
image_file.write(bytes_data)
st.image(uploaded_file, caption="Uploaded image.",
use_column_width=True)
scenario = image_to_text(uploaded_file.name)
story = generate_story(scenario)
audio_file_name = generate_speech(story)
with st.expander("scenario"):
st.write(scenario)
with st.expander("story"):
st.write(story)
st.audio(audio_file_name)
if __name__ == "__main__":
main()