Joseph Pollack commited on
Commit
31cdfbf
·
unverified ·
1 Parent(s): 709ae40

decodes outputs using tokenizer

Browse files
Files changed (1) hide show
  1. app.py +32 -8
app.py CHANGED
@@ -113,16 +113,40 @@ class LOperatorDemo:
113
  tokenize=True,
114
  )
115
 
116
- logger.info(f"Processor output type: {type(inputs)}")
117
 
118
- # If processor returns a string, just return it directly
119
- if isinstance(inputs, str):
120
- logger.info("Processor returned string, returning directly...")
121
- return inputs
122
 
123
- # If it's a dict or other type, convert to string and return
124
- logger.info("Converting processor output to string...")
125
- return str(inputs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  except Exception as e:
128
  logger.error(f"Error generating action: {str(e)}")
 
113
  tokenize=True,
114
  )
115
 
116
+ logger.info(f"Processor output keys: {list(inputs.keys())}")
117
 
118
+ # Move inputs to device
119
+ for key, value in inputs.items():
120
+ if isinstance(value, torch.Tensor):
121
+ inputs[key] = value.to(self.model.device)
122
 
123
+ logger.info(f"Inputs shape: {inputs['input_ids'].shape}, device: {inputs['input_ids'].device}")
124
+
125
+ # Generate response
126
+ logger.info("Generating response...")
127
+ with torch.no_grad():
128
+ outputs = self.model.generate(
129
+ **inputs,
130
+ max_new_tokens=128,
131
+ do_sample=True,
132
+ temperature=0.7,
133
+ top_p=0.9,
134
+ pad_token_id=self.processor.tokenizer.eos_token_id
135
+ )
136
+
137
+ logger.info("Decoding response...")
138
+ # Decode the generated tokens
139
+ response = self.processor.tokenizer.decode(
140
+ outputs[0][inputs['input_ids'].shape[1]:],
141
+ skip_special_tokens=True
142
+ )
143
+
144
+ # Try to parse as JSON for better formatting
145
+ try:
146
+ parsed_response = json.loads(response)
147
+ return json.dumps(parsed_response, indent=2)
148
+ except:
149
+ return response
150
 
151
  except Exception as e:
152
  logger.error(f"Error generating action: {str(e)}")