davidr99 commited on
Commit
8e76f41
·
verified ·
1 Parent(s): f0faebf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ from unsloth import FastVisionModel
3
+ model, tokenizer = FastVisionModel.from_pretrained(
4
+ model_name = "davidr99/qwen2-7b-instruct-blackjack", # YOUR MODEL YOU USED FOR TRAINING
5
+ load_in_4bit = True, # Set to False for 16bit LoRA
6
+ )
7
+ FastVisionModel.for_inference(model) # Enable for inference!
8
+
9
+ import gradio as gr
10
+
11
+ def blackjack_ai(image):
12
+ image = Image.fromarray(image.astype('uint8'), 'RGB')
13
+ instruction = "Write the LaTeX representation for this image."
14
+
15
+ messages = [
16
+ {"role": "user", "content": [
17
+ {"type": "image"},
18
+ {"type": "text", "text": instruction}
19
+ ]}
20
+ ]
21
+ input_text = tokenizer.apply_chat_template(messages, add_generation_prompt = True)
22
+ inputs = tokenizer(
23
+ image,
24
+ input_text,
25
+ add_special_tokens = False,
26
+ return_tensors = "pt",
27
+ ).to("cuda")
28
+
29
+ from transformers import TextStreamer
30
+ text_streamer = TextStreamer(tokenizer, skip_prompt = True)
31
+ _ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 128,
32
+ use_cache = True, temperature = 1.5, min_p = 0.1)
33
+
34
+ return text_streamer
35
+
36
+ with gr.Blocks() as demo:
37
+
38
+ image = gr.Image()
39
+ submit = gr.Button("Submit")
40
+ output = gr.TextArea()
41
+
42
+ submit.click(blackjack_ai, inputs=[image], outputs=[output])
43
+
44
+ demo.launch()