Spaces:
Running
Running
Commit
·
b6863da
1
Parent(s):
6151483
Initial Call to llava
Browse files- .gitignore +1 -0
- app.py +45 -4
- requirements.txt +4 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.idea/
|
app.py
CHANGED
|
@@ -1,7 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
-
demo.launch()
|
|
|
|
| 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, AutoModelForCausalLM
|
| 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 = AutoModelForCausalLM.from_pretrained(model_id,
|
| 18 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 19 |
+
device_map="auto")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def chat_with_llava(image, question, history=[]):
|
| 23 |
+
if image is None or not question.strip():
|
| 24 |
+
return history + [[question, "Please provide both an image and a prompt."]]
|
| 25 |
+
|
| 26 |
+
# Preprocess input
|
| 27 |
+
inputs = processor(text=question, images=image, return_tensors="pt").to(model.device)
|
| 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 a palm image and ask a question. The LLaVA model will respond like a palmistry expert!",
|
| 45 |
+
)
|
| 46 |
|
| 47 |
+
chat_interface.launch()
|
|
|
|
| 48 |
|
|
|
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
Pillow
|
| 4 |
+
gradio
|