LuisV
commited on
Commit
•
60fb1ce
1
Parent(s):
a883cc0
adding main captioning functionality
Browse files- app.py +65 -0
- imageprocessing/imageprocessingtools.py +1 -1
- prompting/promptingutils.py +54 -54
app.py
CHANGED
@@ -1,4 +1,69 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
def greet(name):
|
4 |
return "Hello " + name + "!!"
|
|
|
1 |
import gradio as gr
|
2 |
+
import os, sys
|
3 |
+
from prompting import promptingutils
|
4 |
+
from imageprocessing import imageprocessingtools
|
5 |
+
from openai import OpenAI
|
6 |
+
from prompting.promptingutils import DEFAULT_N_SAMPLES, DEFAULT_OBJECT_THRESHOLD, DEFAULT_RANDOM_STATE
|
7 |
+
|
8 |
+
|
9 |
+
AVAILABLE_LLMS = [
|
10 |
+
"vicuna-7b",
|
11 |
+
"llama-7b-chat",
|
12 |
+
"mistral-7b-instruct",
|
13 |
+
"vicuna-13b",
|
14 |
+
]
|
15 |
+
|
16 |
+
DEFAULT_TEMPERATURE = 0
|
17 |
+
LLAMA_API_TOKEN = os.environ["LLAMA_API_TOKEN"]
|
18 |
+
|
19 |
+
client = OpenAI(
|
20 |
+
api_key = LLAMA_API_TOKEN,
|
21 |
+
base_url = "https://api.llama-api.com"
|
22 |
+
)
|
23 |
+
|
24 |
+
|
25 |
+
def caption_artwork(
|
26 |
+
image_filepath: os.PathLike,
|
27 |
+
llm :str,
|
28 |
+
temperature = DEFAULT_TEMPERATURE,
|
29 |
+
items_threshold = DEFAULT_OBJECT_THRESHOLD,
|
30 |
+
random_state = DEFAULT_RANDOM_STATE,
|
31 |
+
n_samples_per_emotion = DEFAULT_N_SAMPLES
|
32 |
+
)-> tuple:
|
33 |
+
|
34 |
+
all_information = imageprocessingtools.extract_all_information_from_image(image_filepath)
|
35 |
+
|
36 |
+
emotion = all_information["emotion"]
|
37 |
+
colors_list = all_information["colors_list"]
|
38 |
+
objects_and_probs = all_information["objects_and_probs"]
|
39 |
+
objects_list = promptingutils.filter_items(objects_and_probs, items_threshold=items_threshold)
|
40 |
+
|
41 |
+
user_prompt = promptingutils.get_user_prompt(
|
42 |
+
colors_list=colors_list,
|
43 |
+
objects_list=objects_list,
|
44 |
+
emotion=emotion,
|
45 |
+
n_samples_per_emotion=n_samples_per_emotion,
|
46 |
+
random_state=random_state,
|
47 |
+
object_threshold=items_threshold
|
48 |
+
|
49 |
+
)
|
50 |
+
|
51 |
+
response = client.chat.completions.create(
|
52 |
+
model = llm,
|
53 |
+
messages = [
|
54 |
+
{"role": "system" , "content": "Assistant is a large language model trained by OpenAI."},
|
55 |
+
{"role": "user" , "content": user_prompt}
|
56 |
+
],
|
57 |
+
temperature = temperature
|
58 |
+
)
|
59 |
+
|
60 |
+
commentary_str = response.choices[0].message.content
|
61 |
+
colors_str = ", ".join(colors_list)
|
62 |
+
objects_str = ", ".join(objects_list)
|
63 |
+
emotion_str = emotion
|
64 |
+
|
65 |
+
return (emotion_str, colors_str, objects_str, commentary_str)
|
66 |
+
|
67 |
|
68 |
def greet(name):
|
69 |
return "Hello " + name + "!!"
|
imageprocessing/imageprocessingtools.py
CHANGED
@@ -51,7 +51,7 @@ def extract_all_information_from_image(
|
|
51 |
)
|
52 |
|
53 |
result = {
|
54 |
-
"
|
55 |
"objects_and_probs" : objects_and_probs,
|
56 |
"emotion": emotion
|
57 |
}
|
|
|
51 |
)
|
52 |
|
53 |
result = {
|
54 |
+
"colors_list": colors,
|
55 |
"objects_and_probs" : objects_and_probs,
|
56 |
"emotion": emotion
|
57 |
}
|
prompting/promptingutils.py
CHANGED
@@ -53,15 +53,15 @@ def fill_extracted_items(
|
|
53 |
def load_dataframe(
|
54 |
csv_filepath,
|
55 |
):
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
|
64 |
-
|
65 |
|
66 |
TOP_COMMENTARIES_DFS = {
|
67 |
emotion : load_dataframe(os.path.join(TOP_COMMENTARIES_DIR, f"top_{emotion.replace(' ', '_')}.csv"))
|
@@ -111,18 +111,18 @@ def get_subprompt_for_emotion(
|
|
111 |
random_state = DEFAULT_RANDOM_STATE,
|
112 |
object_threshold = DEFAULT_OBJECT_THRESHOLD,
|
113 |
):
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
|
125 |
-
|
126 |
|
127 |
|
128 |
def get_subprompt_with_examples(
|
@@ -153,38 +153,38 @@ def get_user_prompt(
|
|
153 |
random_state = DEFAULT_RANDOM_STATE,
|
154 |
object_threshold = DEFAULT_OBJECT_THRESHOLD,
|
155 |
):
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
|
|
53 |
def load_dataframe(
|
54 |
csv_filepath,
|
55 |
):
|
56 |
+
df = pd.read_csv(csv_filepath, index_col = 0)
|
57 |
+
for stringified_col in [
|
58 |
+
"maskrcnn_objects",
|
59 |
+
"colors",
|
60 |
+
"clip_recognized_objects",
|
61 |
+
]:
|
62 |
+
df[stringified_col] = df[stringified_col].apply(eval)
|
63 |
|
64 |
+
return df
|
65 |
|
66 |
TOP_COMMENTARIES_DFS = {
|
67 |
emotion : load_dataframe(os.path.join(TOP_COMMENTARIES_DIR, f"top_{emotion.replace(' ', '_')}.csv"))
|
|
|
111 |
random_state = DEFAULT_RANDOM_STATE,
|
112 |
object_threshold = DEFAULT_OBJECT_THRESHOLD,
|
113 |
):
|
114 |
+
random_samples = get_random_samples_for_emotion(
|
115 |
+
emotion = emotion,
|
116 |
+
n_samples = n_samples,
|
117 |
+
random_state = random_state,
|
118 |
+
object_threshold=object_threshold,
|
119 |
+
)
|
120 |
+
subprompt = [
|
121 |
+
fill_extracted_items(**entry) for entry in random_samples
|
122 |
+
]
|
123 |
+
subprompt = "\n".join(subprompt)
|
124 |
|
125 |
+
return subprompt
|
126 |
|
127 |
|
128 |
def get_subprompt_with_examples(
|
|
|
153 |
random_state = DEFAULT_RANDOM_STATE,
|
154 |
object_threshold = DEFAULT_OBJECT_THRESHOLD,
|
155 |
):
|
156 |
+
user_prompt= (
|
157 |
+
"You have to write a commentary for an artwork.\n"
|
158 |
+
"To write the commentary, you are given the objects present in the picture, "
|
159 |
+
"the colors present in the picture, and the emotion the picture evokes.\n"
|
160 |
+
"You are first shown several examples, and then have to give your commentary.\n"
|
161 |
+
"First come the examples, and then the objects, colors, and emotion you will have to use for your commentary.\n"
|
162 |
+
"Avoid explicitly mentioning the objects, or colors, or emotion, if it sounds more natural.\n"
|
163 |
+
"Only write the commentary.\n"
|
164 |
+
"\n"
|
165 |
+
"EXAMPLES:"
|
166 |
+
"\n\n"
|
167 |
+
"{examples}"
|
168 |
+
"\n"
|
169 |
+
"Now, write your personal opinion about the picture."
|
170 |
+
"\n"
|
171 |
+
"{image_subprompt}"
|
172 |
+
)
|
173 |
+
|
174 |
+
examples = get_subprompt_with_examples(
|
175 |
+
n_samples_per_emotion = n_samples_per_emotion,
|
176 |
+
random_state = random_state,
|
177 |
+
object_threshold=object_threshold,
|
178 |
+
)
|
179 |
+
|
180 |
+
image_subprompt = fill_extracted_items(
|
181 |
+
colors_list = colors_list,
|
182 |
+
objects_list = objects_list,
|
183 |
+
emotion = emotion,
|
184 |
+
commentary = None,
|
185 |
+
)
|
186 |
+
|
187 |
+
|
188 |
+
result = user_prompt.format(examples = examples, image_subprompt = image_subprompt)
|
189 |
+
|
190 |
+
return result
|