File size: 2,006 Bytes
b0f6ad7 63eb096 5429a99 63eb096 cb0665f b0f6ad7 63eb096 5429a99 cb0665f 5429a99 cb0665f d5986ec 63eb096 b7b26d6 63eb096 68763ab b7b26d6 5429a99 b7b26d6 5429a99 b7b26d6 5429a99 b7b26d6 5429a99 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import streamlit as st
from transformers import pipeline
from huggingface_hub import login, InferenceClient
from PIL import Image
import os
login(token=os.getenv("HUGGINGFACE_TOKEN"))
client = InferenceClient(api_key="HUGGINGFACE_TOKEN")
st.header("Character Captions (IN PROGRESS!)")
st.write("Have a character caption any image you upload!")
character = st.selectbox("Choose a character", ["rapper", "monkey", "shrek", "unintelligible"])
uploaded_img = st.file_uploader("Upload an image")
if uploaded_img is not None:
image = Image.open(uploaded_img)
st.image(image)
image_captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
response = image_captioner(image)
caption = response[0]['generated_text']
st.write("Caption:", caption)
character_prompts = {
"rapper": f"Describe this scene like you're a rapper: {caption}.",
"monkey": f"Describe this scene like you're a monkey going bananas: {caption}.",
"shrek": f"Describe this scene like you're Shrek: {caption}.",
"unintelligible": f"Describe this scene in a way that makes no sense: {caption}."
}
prompt = character_prompts[character]
st.write(prompt)
personality = "rapper"
prompt = character_prompts[personality]
messages = [
{ "role": "user", "content": prompt }
]
stream = client.chat.completions.create(
model="meta-llama/Llama-3.2-3B-Instruct",
messages=messages,
max_tokens=500,
stream=True
)
for chunk in stream:
st.write(chunk.choices[0].delta.content)
# text_generator = pipeline("text-generation", model="meta-llama/Llama-2-7b-hf", framework="pt")
# prompt = character_prompts[character]
# st.write("Styled Prompt:", prompt)
# generated_text = text_generator(prompt, max_length=50, do_sample=True)
# styled_caption = generated_text[0]['generated_text']
# st.write("Character-Styled Caption:", styled_caption) |