Spaces:
Running
Running
File size: 1,975 Bytes
6151483 289622b b6863da 2ce26d8 e594f31 b6863da 289622b b6863da 2ce26d8 b6863da 2ce26d8 289622b b6863da 2ce26d8 b6863da 2ce26d8 |
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 |
import gradio as gr
from transformers import AutoProcessor, LlavaForConditionalGeneration
from PIL import Image
import torch
# Load model & processor
# model_id = "llava-hf/llava-1.5-7b-hf" # Exceeding 16 GB Memory
model_id = "llava-hf/llava-1.5-7b-hf-int4"
processor = AutoProcessor.from_pretrained(model_id)
model = LlavaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
low_cpu_mem_usage=True,
device_map="auto",
)
# Main prediction function
def analyze_palm(image, question, history):
if image is None or not question.strip():
history.append((question, "Please provide both image and question."))
return history, ""
conversation = [
{"role": "user", "content": [
{"type": "text", "text": question},
{"type": "image"}
]}
]
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=512)
response = processor.decode(output[0], skip_special_tokens=True)
history.append((question, response))
return history, ""
# Build UI using Blocks
with gr.Blocks() as demo:
gr.Markdown("## ๐๏ธ AI Palm Reader\nUpload a palm image and ask a question. Get a palmistry-style response.")
with gr.Row():
with gr.Column(scale=1):
image_input = gr.Image(type="pil", label="Palm Image")
prompt_input = gr.Textbox(lines=2, label="Your Question", placeholder="What does my palm say?")
submit_btn = gr.Button("Ask")
with gr.Column(scale=2):
chatbot = gr.Chatbot(label="Palmistry Chat")
state = gr.State([])
submit_btn.click(
fn=analyze_palm,
inputs=[image_input, prompt_input, state],
outputs=[chatbot, prompt_input]
)
demo.launch()
|