khurrameycon commited on
Commit
112e707
·
verified ·
1 Parent(s): 2bedcbe

response updated

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -67,11 +67,14 @@ processor = AutoProcessor.from_pretrained(model_name, use_auth_token=HF_TOKEN)
67
  def predict_image(image_url, text):
68
  try:
69
  # Download the image from the URL
 
 
 
 
70
  response = requests.get(image_url)
71
  response.raise_for_status() # Raise an error for invalid responses
72
  image = Image.open(io.BytesIO(response.content)).convert("RGB")
73
-
74
- # Prepare the input messages
75
  messages = [
76
  {"role": "user", "content": [
77
  {"type": "image"}, # Specify that an image is provided
@@ -83,7 +86,26 @@ def predict_image(image_url, text):
83
  input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
84
 
85
  # Process the inputs and move to the appropriate device
86
- inputs = processor(image=image, text=input_text, return_tensors="pt").to("cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  # Generate a response from the model
89
  # outputs = model.generate(**inputs, max_new_tokens=100)
@@ -91,23 +113,23 @@ def predict_image(image_url, text):
91
  # # Decode the output to return the final response
92
  # response = processor.decode(outputs[0], skip_special_tokens=True)
93
 
94
- streamer = TextIteratorStreamer(processor, skip_special_tokens=True, skip_prompt=True)
95
 
96
- generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=2048)
97
- generated_text = ""
98
 
99
- thread = Thread(target=model.generate, kwargs=generation_kwargs)
100
- thread.start()
101
- buffer = ""
102
-
103
- for new_text in streamer:
104
- buffer += new_text
105
- # generated_text_without_prompt = buffer
106
- # # time.sleep(0.01)
107
- # yield buffer
108
 
109
- return buffer
110
- # return response
111
 
112
  except Exception as e:
113
  raise ValueError(f"Error during prediction: {str(e)}")
 
67
  def predict_image(image_url, text):
68
  try:
69
  # Download the image from the URL
70
+ # response = requests.get(image_url)
71
+ # response.raise_for_status() # Raise an error for invalid responses
72
+ # image = Image.open(io.BytesIO(response.content)).convert("RGB")
73
+
74
  response = requests.get(image_url)
75
  response.raise_for_status() # Raise an error for invalid responses
76
  image = Image.open(io.BytesIO(response.content)).convert("RGB")
77
+
 
78
  messages = [
79
  {"role": "user", "content": [
80
  {"type": "image"}, # Specify that an image is provided
 
86
  input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
87
 
88
  # Process the inputs and move to the appropriate device
89
+ inputs = processor(image, input_text, return_tensors="pt").to(device)
90
+
91
+ outputs = model.generate(**inputs, max_new_tokens=100)
92
+
93
+ # Decode the output to return the final response
94
+ response = processor.decode(outputs[0], skip_special_tokens=True)
95
+
96
+ # # Prepare the input messages
97
+ # messages = [
98
+ # {"role": "user", "content": [
99
+ # {"type": "image"}, # Specify that an image is provided
100
+ # {"type": "text", "text": text} # Add the user-provided text input
101
+ # ]}
102
+ # ]
103
+
104
+ # # Create the input text using the processor's chat template
105
+ # input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
106
+
107
+ # # Process the inputs and move to the appropriate device
108
+ # inputs = processor(image=image, text=input_text, return_tensors="pt").to("cuda")
109
 
110
  # Generate a response from the model
111
  # outputs = model.generate(**inputs, max_new_tokens=100)
 
113
  # # Decode the output to return the final response
114
  # response = processor.decode(outputs[0], skip_special_tokens=True)
115
 
116
+ # streamer = TextIteratorStreamer(processor, skip_special_tokens=True, skip_prompt=True)
117
 
118
+ # generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=2048)
119
+ # generated_text = ""
120
 
121
+ # thread = Thread(target=model.generate, kwargs=generation_kwargs)
122
+ # thread.start()
123
+ # buffer = ""
124
+
125
+ # for new_text in streamer:
126
+ # buffer += new_text
127
+ # # generated_text_without_prompt = buffer
128
+ # # # time.sleep(0.01)
129
+ # # yield buffer
130
 
131
+ # return buffer
132
+ return response
133
 
134
  except Exception as e:
135
  raise ValueError(f"Error during prediction: {str(e)}")