Spaces:
Running
Running
Commit
·
2ce26d8
1
Parent(s):
8cbaeb7
Updated app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from transformers import AutoProcessor, LlavaForConditionalGeneration
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
|
|
|
| 6 |
model_id = "llava-hf/llava-1.5-7b-hf"
|
| 7 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 8 |
model = LlavaForConditionalGeneration.from_pretrained(
|
|
@@ -12,12 +13,13 @@ model = LlavaForConditionalGeneration.from_pretrained(
|
|
| 12 |
device_map="auto",
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
if image is None or not question.strip():
|
| 17 |
-
history.append(
|
| 18 |
-
return history
|
| 19 |
|
| 20 |
-
# Format multimodal prompt
|
| 21 |
conversation = [
|
| 22 |
{"role": "user", "content": [
|
| 23 |
{"type": "text", "text": question},
|
|
@@ -25,19 +27,34 @@ def chat_with_llava(image, question, history=[]):
|
|
| 25 |
]}
|
| 26 |
]
|
| 27 |
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
| 28 |
-
|
| 29 |
-
# Encode inputs
|
| 30 |
inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
|
| 31 |
-
outputs = model.generate(**inputs, max_new_tokens=512)
|
| 32 |
-
answer = processor.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Load model & processor
|
| 7 |
model_id = "llava-hf/llava-1.5-7b-hf"
|
| 8 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 9 |
model = LlavaForConditionalGeneration.from_pretrained(
|
|
|
|
| 13 |
device_map="auto",
|
| 14 |
)
|
| 15 |
|
| 16 |
+
|
| 17 |
+
# Main prediction function
|
| 18 |
+
def analyze_palm(image, question, history):
|
| 19 |
if image is None or not question.strip():
|
| 20 |
+
history.append((question, "Please provide both image and question."))
|
| 21 |
+
return history, ""
|
| 22 |
|
|
|
|
| 23 |
conversation = [
|
| 24 |
{"role": "user", "content": [
|
| 25 |
{"type": "text", "text": question},
|
|
|
|
| 27 |
]}
|
| 28 |
]
|
| 29 |
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
|
|
|
|
|
|
| 30 |
inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
output = model.generate(**inputs, max_new_tokens=512)
|
| 33 |
+
response = processor.decode(output[0], skip_special_tokens=True)
|
| 34 |
|
| 35 |
+
history.append((question, response))
|
| 36 |
+
return history, ""
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Build UI using Blocks
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("## 🖐️ AI Palm Reader\nUpload a palm image and ask a question. Get a palmistry-style response.")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column(scale=1):
|
| 45 |
+
image_input = gr.Image(type="pil", label="Palm Image")
|
| 46 |
+
prompt_input = gr.Textbox(lines=2, label="Your Question", placeholder="What does my palm say?")
|
| 47 |
+
submit_btn = gr.Button("Ask")
|
| 48 |
+
|
| 49 |
+
with gr.Column(scale=2):
|
| 50 |
+
chatbot = gr.Chatbot(label="Palmistry Chat")
|
| 51 |
+
|
| 52 |
+
state = gr.State([])
|
| 53 |
+
|
| 54 |
+
submit_btn.click(
|
| 55 |
+
fn=analyze_palm,
|
| 56 |
+
inputs=[image_input, prompt_input, state],
|
| 57 |
+
outputs=[chatbot, prompt_input]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
demo.launch()
|