Nightwalkx commited on
Commit
677a9ea
·
1 Parent(s): 493162e

Add application file

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline, BitsAndBytesConfig
3
+ import torch
4
+
5
+ quantization_config = BitsAndBytesConfig(
6
+ load_in_4bit=True,
7
+ bnb_4bit_compute_dtype=torch.bfloat16
8
+ )
9
+
10
+ model_id = "rogerxi/llava-finetune-test"
11
+ pipe = pipeline("image-to-text", model=model_id, model_kwargs={"quantization_config": quantization_config})
12
+
13
+ def update_conversation(new_message, history, image):
14
+
15
+ if image is None:
16
+ return "Please upload an image first using the widget on the left"
17
+
18
+ prompt = "USER: <image>\n"
19
+
20
+ for i in range(len(history)):
21
+ prompt += history[i][0] + '\nASSISTANT: ' + history[i][1] + "\nUSER: "
22
+
23
+ prompt = prompt + new_message + '\nASSISTANT: '
24
+
25
+ outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})[0]['generated_text']
26
+
27
+ return outputs[len(prompt) - 6:]
28
+
29
+ with gr.Blocks() as demo:
30
+
31
+ with gr.Row():
32
+ image = gr.Image(type='pil', interactive=True)
33
+
34
+ gr.ChatInterface(
35
+ update_conversation, additional_inputs=[image]
36
+ )
37
+
38
+ demo.launch(debug=True)