shcho-isle commited on
Commit
3902745
·
verified ·
1 Parent(s): 42a9b9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -8
app.py CHANGED
@@ -1,13 +1,56 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
 
3
 
4
- tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-VL-72B-Instruct")
5
- model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-VL-72B-Instruct")
 
 
 
6
 
7
- def predict(input_text):
8
- inputs = tokenizer(input_text, return_tensors="pt")
9
- outputs = model.generate(**inputs)
10
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- interface = gr.Interface(fn=predict, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  interface.launch()
 
1
  import gradio as gr
2
+ from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
3
+ from qwen_vl_utils import process_vision_info
4
 
5
+ # Load the model and processor
6
+ model = Qwen2VLForConditionalGeneration.from_pretrained(
7
+ "Qwen/Qwen2-VL-72B-Instruct", torch_dtype="auto", device_map="auto"
8
+ )
9
+ processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-72B-Instruct")
10
 
11
+ # Define a function to process input and generate a response
12
+ def generate_response(image, text):
13
+ # Prepare the input
14
+ messages = [
15
+ {
16
+ "role": "user",
17
+ "content": [
18
+ {"type": "image", "image": image},
19
+ {"type": "text", "text": text},
20
+ ],
21
+ }
22
+ ]
23
+
24
+ # Process the input data
25
+ text_data = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
26
+ image_inputs, video_inputs = process_vision_info(messages)
27
+ inputs = processor(
28
+ text=[text_data],
29
+ images=image_inputs,
30
+ videos=video_inputs,
31
+ padding=True,
32
+ return_tensors="pt",
33
+ )
34
 
35
+ # Generate the output
36
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
37
+ generated_ids_trimmed = [
38
+ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
39
+ ]
40
+ output_text = processor.batch_decode(
41
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
42
+ )
43
+
44
+ return output_text[0]
45
+
46
+ # Create the Gradio interface
47
+ interface = gr.Interface(
48
+ fn=generate_response,
49
+ inputs=[gr.Image(type="pil", label="Input Image"), gr.Textbox(label="Input Text")],
50
+ outputs="text",
51
+ title="Qwen2-VL-72B-Instruct",
52
+ description="Generate AI responses based on image and text input using Qwen2-VL-72B-Instruct.",
53
+ )
54
+
55
+ # Launch the app
56
  interface.launch()