Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
15 |
|
16 |
-
# Model and
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
trust_remote_code=True
|
20 |
)
|
21 |
|
22 |
-
model =
|
23 |
-
|
24 |
trust_remote_code=True,
|
25 |
-
device_map="auto"
|
26 |
-
|
|
|
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
|
43 |
if text_input:
|
44 |
-
|
45 |
else:
|
46 |
-
|
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 |
-
#
|
55 |
-
response =
|
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:
|