Update1
Browse files
app.py
CHANGED
@@ -4,62 +4,61 @@ from huggingface_hub import InferenceClient
|
|
4 |
from PIL import Image
|
5 |
import os
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
st.session_state['
|
12 |
-
st.session_state['
|
|
|
|
|
13 |
|
14 |
def main():
|
15 |
-
|
16 |
-
|
17 |
-
st.
|
18 |
-
st.
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
["goku", "elmo", "kirby", "pikachu"]
|
23 |
-
)
|
24 |
-
|
25 |
-
uploaded_img = st.file_uploader("Share your image!")
|
26 |
-
|
27 |
if uploaded_img is not None:
|
28 |
image = Image.open(uploaded_img)
|
29 |
st.image(image)
|
|
|
30 |
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
"goku": f"Describe this image like you're Goku from Dragon Ball Z, mentioning power levels: {base_caption}",
|
38 |
-
"elmo": f"Describe this image like you're Elmo from Sesame Street, speaking in third person: {base_caption}",
|
39 |
-
"kirby": f"Describe this image like you're Kirby, being cute and mentioning food: {base_caption}",
|
40 |
-
"pikachu": f"Describe this image like you're Pikachu, using 'pika' frequently: {base_caption}"
|
41 |
}
|
42 |
-
|
|
|
43 |
messages = [
|
44 |
-
{
|
45 |
-
"role": "user",
|
46 |
-
"content": character_reactions[character]
|
47 |
-
}
|
48 |
]
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
messages=messages,
|
54 |
max_tokens=500,
|
55 |
stream=True
|
56 |
)
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
st.write(character_response)
|
63 |
|
64 |
if __name__ == '__main__':
|
65 |
main()
|
|
|
4 |
from PIL import Image
|
5 |
import os
|
6 |
|
7 |
+
|
8 |
+
def initialize():
|
9 |
+
if 'initialized' not in st.session_state:
|
10 |
+
print("Initializing...")
|
11 |
+
st.session_state['initialized'] = True
|
12 |
+
st.session_state['api_key'] = os.getenv("HUGGINGFACE_TOKEN")
|
13 |
+
st.session_state['client'] = InferenceClient(api_key=st.session_state['api_key'])
|
14 |
+
|
15 |
|
16 |
def main():
|
17 |
+
initialize()
|
18 |
+
st.header("Character Captions")
|
19 |
+
st.write("Have a character caption any image you upload!")
|
20 |
+
character = st.selectbox("Choose a character", ["artist", "elmo", "unintelligible", "goku"])
|
21 |
+
|
22 |
+
uploaded_img = st.file_uploader("Upload an image here")
|
23 |
+
|
|
|
|
|
|
|
|
|
|
|
24 |
if uploaded_img is not None:
|
25 |
image = Image.open(uploaded_img)
|
26 |
st.image(image)
|
27 |
+
|
28 |
|
29 |
+
image_captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
|
30 |
+
response = image_captioner(image)
|
31 |
+
caption = response[0]['generated_text']
|
32 |
+
|
33 |
+
|
34 |
|
35 |
+
character_prompts = {
|
36 |
+
"artist": f"Describe this caption like you're a artist: {caption}.",
|
37 |
+
"elmo": f"Describe this caption like you're elmo: {caption}.",
|
38 |
+
"unintelligible": f"Describe this caption in a way that makes no sense: {caption}.",
|
39 |
+
"goku": f"Describe this caption like you're goku: {caption}."
|
|
|
|
|
|
|
|
|
40 |
}
|
41 |
+
|
42 |
+
prompt = character_prompts[character]
|
43 |
messages = [
|
44 |
+
{ "role": "user", "content": prompt }
|
|
|
|
|
|
|
45 |
]
|
46 |
+
|
47 |
|
48 |
+
stream = st.session_state['client'].chat.completions.create(
|
49 |
+
model="meta-llama/Llama-3.2-3B-Instruct",
|
50 |
+
messages=messages,
|
|
|
51 |
max_tokens=500,
|
52 |
stream=True
|
53 |
)
|
54 |
+
|
55 |
+
response = ''
|
56 |
+
for chunk in stream:
|
57 |
+
response += chunk.choices[0].delta.content
|
58 |
|
59 |
+
st.write(response)
|
60 |
+
|
61 |
+
|
|
|
|
|
62 |
|
63 |
if __name__ == '__main__':
|
64 |
main()
|