Spaces:
Sleeping
Sleeping
added main
Browse files
app.py
CHANGED
|
@@ -5,49 +5,62 @@ from PIL import Image
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
st.
|
| 12 |
-
st.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
image
|
| 20 |
-
st.
|
| 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 |
|
|
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
|
| 8 |
+
def initialize():
|
| 9 |
+
if 'initialized' not in st.session_state: # Initialize only once
|
| 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", ["rapper", "shrek", "unintelligible", "cookie monster"])
|
| 21 |
+
|
| 22 |
+
uploaded_img = st.file_uploader("Upload an image")
|
| 23 |
+
|
| 24 |
+
if uploaded_img is not None:
|
| 25 |
+
# Open Image
|
| 26 |
+
image = Image.open(uploaded_img)
|
| 27 |
+
st.image(image)
|
| 28 |
+
|
| 29 |
+
# Get caption from image
|
| 30 |
+
image_captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
|
| 31 |
+
response = image_captioner(image)
|
| 32 |
+
caption = response[0]['generated_text']
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Pass the caption to a character prompt
|
| 36 |
+
character_prompts = {
|
| 37 |
+
"rapper": f"Describe this caption like you're a rapper: {caption}.",
|
| 38 |
+
"shrek": f"Describe this caption like you're Shrek: {caption}.",
|
| 39 |
+
"unintelligible": f"Describe this caption in a way that makes no sense: {caption}.",
|
| 40 |
+
"cookie monster": f"Describe this caption like you're cookie monster: {caption}."
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
prompt = character_prompts[character]
|
| 44 |
+
messages = [
|
| 45 |
+
{ "role": "user", "content": prompt }
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
# Pass to Llama for character output regarding image caption
|
| 49 |
+
stream = st.session_state['client'].chat.completions.create(
|
| 50 |
+
model="meta-llama/Llama-3.2-3B-Instruct",
|
| 51 |
+
messages=messages,
|
| 52 |
+
max_tokens=500,
|
| 53 |
+
stream=True
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
response = ''
|
| 57 |
+
for chunk in stream:
|
| 58 |
+
response += chunk.choices[0].delta.content
|
| 59 |
+
|
| 60 |
+
st.write(response)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
if __name__ == '__main__':
|
| 65 |
+
main()
|
| 66 |
|