zforkash commited on
Commit
baa9321
1 Parent(s): fc17b4b
Files changed (1) hide show
  1. app.py +40 -41
app.py CHANGED
@@ -4,62 +4,61 @@ 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()
 
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()