Spaces:
Sleeping
Sleeping
peterpeter8585
commited on
Commit
•
67547b3
1
Parent(s):
6e5a3ee
Update app.py
Browse files
app.py
CHANGED
@@ -47,32 +47,56 @@ def respond1(
|
|
47 |
yield response
|
48 |
def respond0(multimodal_input,password):
|
49 |
if password==password1:
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
|
78 |
|
|
|
47 |
yield response
|
48 |
def respond0(multimodal_input,password):
|
49 |
if password==password1:
|
50 |
+
if multimodal_input["files"] ==not None:
|
51 |
|
52 |
+
images = multimodal_input["files"]
|
53 |
+
content = [{"type": "image"} for _ in images]
|
54 |
+
content.append({"type": "text", "text": multimodal_input["text"]})
|
55 |
+
messages=[{"role":"system", "content":[{"type":"text", "text":"Your name is Chatchat.And, your made by SungYoon.In Korean, 정성윤.And these are the instructions:"+"You are a helpful assietant."}]}]
|
56 |
+
messages.append([{"role": "user", "content": content}])
|
57 |
+
response = ""
|
58 |
+
|
59 |
+
model_id = "HuggingFaceM4/idefics2-8b"
|
60 |
+
|
61 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
62 |
+
model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype=torch.float16)
|
63 |
+
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
64 |
+
inputs = processor(text=prompt, images=[images], return_tensors="pt")
|
65 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
66 |
+
num_tokens = len(inputs["input_ids"][0])
|
67 |
+
with torch.inference_mode():
|
68 |
+
generated_ids = model.generate(**inputs, max_new_tokens=max_tokens,top_p=top_p, temperature=1.0,)
|
69 |
+
|
70 |
+
new_tokens = generated_ids[:, num_tokens:]
|
71 |
+
generated_text = processor.batch_decode(new_tokens, skip_special_tokens=True)[0]
|
72 |
+
|
73 |
+
token = generated_text
|
74 |
+
response+=token
|
75 |
+
yield response
|
76 |
+
else:
|
77 |
+
|
78 |
+
content={"type": "text", "text": multimodal_input["text"]}
|
79 |
+
messages=[{"role":"system", "content":[{"type":"text", "text":"Your name is Chatchat.And, your made by SungYoon.In Korean, 정성윤.And these are the instructions:"+"You are a helpful assietant."}]}]
|
80 |
+
messages.append([{"role": "user", "content": content}])
|
81 |
+
response = ""
|
82 |
+
|
83 |
+
model_id = "HuggingFaceM4/idefics2-8b"
|
84 |
+
|
85 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
86 |
+
model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype=torch.float16)
|
87 |
+
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
88 |
+
inputs = processor(text=prompt, images=[images], return_tensors="pt")
|
89 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
90 |
+
num_tokens = len(inputs["input_ids"][0])
|
91 |
+
with torch.inference_mode():
|
92 |
+
generated_ids = model.generate(**inputs, max_new_tokens=max_tokens,top_p=top_p, temperature=1.0,)
|
93 |
+
|
94 |
+
new_tokens = generated_ids[:, num_tokens:]
|
95 |
+
generated_text = processor.batch_decode(new_tokens, skip_special_tokens=True)[0]
|
96 |
+
|
97 |
+
token = generated_text
|
98 |
+
response+=token
|
99 |
+
yield response
|
100 |
|
101 |
|
102 |
|