Spaces:
Running
Running
Commit
Β·
289622b
1
Parent(s):
b6863da
Llava Class config updated
Browse files
app.py
CHANGED
|
@@ -1,48 +1,43 @@
|
|
| 1 |
-
# import gradio as gr
|
| 2 |
-
#
|
| 3 |
-
# def greet(name):
|
| 4 |
-
# return "Hello " + name + "!!"
|
| 5 |
-
#
|
| 6 |
-
# demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
-
# demo.launch()
|
| 8 |
-
|
| 9 |
import gradio as gr
|
| 10 |
-
from transformers import AutoProcessor,
|
| 11 |
from PIL import Image
|
| 12 |
import torch
|
| 13 |
|
| 14 |
-
# Load LLaVA model
|
| 15 |
model_id = "llava-hf/llava-1.5-7b-hf"
|
| 16 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 17 |
-
model =
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def chat_with_llava(image, question, history=[]):
|
| 23 |
if image is None or not question.strip():
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# Generate output
|
| 30 |
-
output = model.generate(**inputs, max_new_tokens=512)
|
| 31 |
-
answer = processor.decode(output[0], skip_special_tokens=True)
|
| 32 |
-
|
| 33 |
-
# Append to chat history
|
| 34 |
history.append([question, answer])
|
| 35 |
return history
|
| 36 |
|
| 37 |
-
|
| 38 |
-
# Create Gradio chatbot interface
|
| 39 |
chat_interface = gr.ChatInterface(
|
| 40 |
fn=chat_with_llava,
|
| 41 |
-
inputs=[gr.Image(type="pil", label="Palm Image"),
|
| 42 |
-
gr.Textbox(label="Your Question", placeholder="What does my palm say about my future?")],
|
| 43 |
title="ποΈ AI Palm Reader",
|
| 44 |
-
description="Upload
|
| 45 |
)
|
| 46 |
-
|
| 47 |
chat_interface.launch()
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
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(
|
| 9 |
+
model_id,
|
| 10 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 11 |
+
low_cpu_mem_usage=True,
|
| 12 |
+
device_map="auto",
|
| 13 |
+
)
|
| 14 |
|
| 15 |
def chat_with_llava(image, question, history=[]):
|
| 16 |
if image is None or not question.strip():
|
| 17 |
+
history.append([question, "Please provide both an image and a question."])
|
| 18 |
+
return history
|
| 19 |
+
|
| 20 |
+
# Format multimodal prompt
|
| 21 |
+
conversation = [
|
| 22 |
+
{"role": "user", "content": [
|
| 23 |
+
{"type": "text", "text": question},
|
| 24 |
+
{"type": "image"}
|
| 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 |
history.append([question, answer])
|
| 35 |
return history
|
| 36 |
|
|
|
|
|
|
|
| 37 |
chat_interface = gr.ChatInterface(
|
| 38 |
fn=chat_with_llava,
|
| 39 |
+
inputs=[gr.Image(type="pil", label="Palm Image"), gr.Textbox(label="Your Question")],
|
|
|
|
| 40 |
title="ποΈ AI Palm Reader",
|
| 41 |
+
description="Upload your palm image and ask a questionβLLaVA will respond with a palmistry-style reading."
|
| 42 |
)
|
|
|
|
| 43 |
chat_interface.launch()
|
|
|