aiqtech commited on
Commit
39761c3
·
verified ·
1 Parent(s): 9256bf2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -11,19 +11,22 @@ import subprocess
11
  subprocess.run('pip install --upgrade transformers', shell=True)
12
  subprocess.run('pip install accelerate', shell=True)
13
 
14
- from transformers import AutoProcessor, AutoModelForVisionEncoderDecoder
15
 
16
- # Model and processor initialization with trust_remote_code=True
17
- processor = AutoProcessor.from_pretrained(
18
- "Qwen/QVQ-72B-Preview",
 
 
19
  trust_remote_code=True
20
  )
21
 
22
- model = AutoModelForVisionEncoderDecoder.from_pretrained(
23
- "Qwen/QVQ-72B-Preview",
24
  trust_remote_code=True,
25
- device_map="auto"
26
- ).eval()
 
27
 
28
  # Footer
29
  footer = """
@@ -39,20 +42,14 @@ def process_image(image, text_input=None):
39
  # Convert image to PIL format
40
  image = Image.fromarray(image).convert("RGB")
41
 
42
- # Prepare inputs
43
  if text_input:
44
- inputs = processor(text=text_input, images=image, return_tensors="pt")
45
  else:
46
- inputs = processor(images=image, return_tensors="pt")
47
-
48
- # Move inputs to the same device as the model
49
- inputs = {k: v.to(model.device) for k, v in inputs.items()}
50
-
51
- # Generate output
52
- outputs = model.generate(**inputs, max_new_tokens=1000)
53
 
54
- # Decode response
55
- response = processor.batch_decode(outputs, skip_special_tokens=True)[0]
56
 
57
  return response
58
  except Exception as e:
 
11
  subprocess.run('pip install --upgrade transformers', shell=True)
12
  subprocess.run('pip install accelerate', shell=True)
13
 
14
+ from transformers import AutoModelForCausalLM, AutoTokenizer
15
 
16
+ # Model and tokenizer initialization
17
+ model_name = "Qwen/QVQ-72B-Preview"
18
+
19
+ tokenizer = AutoTokenizer.from_pretrained(
20
+ model_name,
21
  trust_remote_code=True
22
  )
23
 
24
+ model = AutoModelForCausalLM.from_pretrained(
25
+ model_name,
26
  trust_remote_code=True,
27
+ device_map="auto",
28
+ torch_dtype=torch.float16
29
+ )
30
 
31
  # Footer
32
  footer = """
 
42
  # Convert image to PIL format
43
  image = Image.fromarray(image).convert("RGB")
44
 
45
+ # Prepare prompt
46
  if text_input:
47
+ prompt = f"<image>Please describe this image and answer: {text_input}</image>"
48
  else:
49
+ prompt = "<image>Please describe this image in detail.</image>"
 
 
 
 
 
 
50
 
51
+ # Generate response
52
+ response = model.chat(tokenizer, prompt, history=[], images=image)
53
 
54
  return response
55
  except Exception as e: