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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -11,18 +11,24 @@ import subprocess
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
@@ -42,14 +48,30 @@ def process_image(image, text_input=None):
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:
 
11
  subprocess.run('pip install --upgrade transformers', shell=True)
12
  subprocess.run('pip install accelerate', shell=True)
13
 
14
+ from transformers import AutoConfig, PreTrainedModel, AutoTokenizer
15
 
16
  # Model and tokenizer initialization
17
  model_name = "Qwen/QVQ-72B-Preview"
18
 
19
+ config = AutoConfig.from_pretrained(
20
+ model_name,
21
+ trust_remote_code=True
22
+ )
23
+
24
  tokenizer = AutoTokenizer.from_pretrained(
25
  model_name,
26
  trust_remote_code=True
27
  )
28
 
29
+ model = PreTrainedModel.from_pretrained(
30
  model_name,
31
+ config=config,
32
  trust_remote_code=True,
33
  device_map="auto",
34
  torch_dtype=torch.float16
 
48
  # Convert image to PIL format
49
  image = Image.fromarray(image).convert("RGB")
50
 
51
+ # Prepare inputs
52
  if text_input:
53
+ messages = [
54
+ {
55
+ "role": "user",
56
+ "content": [
57
+ {"image": image},
58
+ {"text": text_input}
59
+ ]
60
+ }
61
+ ]
62
  else:
63
+ messages = [
64
+ {
65
+ "role": "user",
66
+ "content": [
67
+ {"image": image},
68
+ {"text": "Please describe this image in detail."}
69
+ ]
70
+ }
71
+ ]
72
 
73
  # Generate response
74
+ response = model.chat(tokenizer, messages=messages)
75
 
76
  return response
77
  except Exception as e: