Spaces:
Runtime error
Runtime error
Create app.py (#3)
Browse files- Create app.py (73bd117f9e65fd5f03983f1d315fafdfe47d6f13)
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Define constants
|
| 6 |
+
MODEL_NAME = "microsoft/Phi-3.5-vision-instruct"
|
| 7 |
+
DESCRIPTION = "# [Phi-3.5-vision Demo](https://huggingface.co/microsoft/Phi-3.5-vision-instruct)"
|
| 8 |
+
DEVICE = "cuda"
|
| 9 |
+
|
| 10 |
+
# Load model and processor
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, trust_remote_code=True, torch_dtype="auto", _attn_implementation="flash_attention_2").to(DEVICE).eval()
|
| 12 |
+
processor = AutoProcessor.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 13 |
+
|
| 14 |
+
def run_example(image, text_input, model_id):
|
| 15 |
+
# Prepare prompt and image for processing
|
| 16 |
+
prompt = f"{text_input}\n"
|
| 17 |
+
image = Image.fromarray(image).convert("RGB")
|
| 18 |
+
|
| 19 |
+
# Process input
|
| 20 |
+
inputs = processor(prompt, image, return_tensors="pt").to(DEVICE)
|
| 21 |
+
generate_ids = model.generate(**inputs, max_new_tokens=1000, eos_token_id=processor.tokenizer.eos_token_id)
|
| 22 |
+
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
|
| 23 |
+
response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 24 |
+
|
| 25 |
+
return response
|
| 26 |
+
|
| 27 |
+
css = """
|
| 28 |
+
#output {
|
| 29 |
+
height: 500px;
|
| 30 |
+
overflow: auto;
|
| 31 |
+
border: 1px solid #ccc;
|
| 32 |
+
}
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
# Set up the Gradio interface
|
| 36 |
+
with gr.Blocks(css=css) as demo:
|
| 37 |
+
gr.Markdown(DESCRIPTION)
|
| 38 |
+
with gr.Tab(label="Phi-3.5 Input"):
|
| 39 |
+
with gr.Row():
|
| 40 |
+
with gr.Column():
|
| 41 |
+
input_img = gr.Image(label="Input Picture")
|
| 42 |
+
text_input = gr.Textbox(label="Question")
|
| 43 |
+
submit_btn = gr.Button(value="Submit")
|
| 44 |
+
with gr.Column():
|
| 45 |
+
output_text = gr.Textbox(label="Output Text")
|
| 46 |
+
submit_btn.click(run_example, inputs=[input_img, text_input, MODEL_NAME], outputs=output_text)
|
| 47 |
+
|
| 48 |
+
demo.launch(debug=True)
|