FirstCommit
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
from PIL import Image
|
5 |
+
import os
|
6 |
+
|
7 |
+
def setup_session():
|
8 |
+
if 'app_ready' not in st.session_state:
|
9 |
+
print("Powering up the Dragon Radar...")
|
10 |
+
st.session_state['app_ready'] = True
|
11 |
+
st.session_state['hf_token'] = os.getenv("HUGGINGFACE_TOKEN")
|
12 |
+
st.session_state['client'] = InferenceClient(api_key=st.session_state['hf_token'])
|
13 |
+
|
14 |
+
def main():
|
15 |
+
setup_session()
|
16 |
+
|
17 |
+
st.header("Anime & Friends Image Commentary")
|
18 |
+
st.write("Let your favorite characters react to any image!")
|
19 |
+
|
20 |
+
character = st.selectbox(
|
21 |
+
"Select your commentator",
|
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 |
+
caption_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
|
33 |
+
base_caption = caption_model(image)[0]['generated_text']
|
34 |
+
|
35 |
+
|
36 |
+
character_reactions = {
|
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 |
+
# Generate character response using Llama
|
51 |
+
response_stream = st.session_state['client'].chat.completions.create(
|
52 |
+
model="meta-llama/Llama-3.2-3B-Instruct",
|
53 |
+
messages=messages,
|
54 |
+
max_tokens=500,
|
55 |
+
stream=True
|
56 |
+
)
|
57 |
+
|
58 |
+
character_response = ''
|
59 |
+
for chunk in response_stream:
|
60 |
+
character_response += chunk.choices[0].delta.content
|
61 |
+
|
62 |
+
st.write(character_response)
|
63 |
+
|
64 |
+
if __name__ == '__main__':
|
65 |
+
main()
|