jarguello76 commited on
Commit
33adf36
·
verified ·
1 Parent(s): 35b6d91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -27,15 +27,23 @@ class HuggingFaceInferenceWrapper:
27
  self.inference_api = inference_api
28
 
29
  def generate(self, prompt: str, **kwargs) -> str:
30
- # Call inference API - returns string directly
31
- response = self.inference_api(inputs=prompt)
32
- if isinstance(response, str):
33
- return response.strip()
34
- elif hasattr(response, 'text'):
35
- return response.text.strip()
36
- else:
37
- # Handle JSON response
38
- return str(response)
 
 
 
 
 
 
 
 
39
 
40
 
41
 
 
27
  self.inference_api = inference_api
28
 
29
  def generate(self, prompt: str, **kwargs) -> str:
30
+ # Call inference API with raw_response=True to get the raw Response object
31
+ response = self.inference_api(inputs=prompt, raw_response=True)
32
+
33
+ # Parse the JSON response
34
+ if hasattr(response, 'json'):
35
+ json_response = response.json()
36
+ # Extract the relevant information from the JSON response
37
+ # This depends on the structure of the JSON response from the API
38
+ # For example, if the response contains a 'generated_text' field:
39
+ if isinstance(json_response, list) and len(json_response) > 0:
40
+ return json_response[0].get('generated_text', '').strip()
41
+ elif isinstance(json_response, dict):
42
+ return json_response.get('generated_text', '').strip()
43
+
44
+ # Fallback: return the raw response text if JSON parsing fails
45
+ return response.text.strip()
46
+
47
 
48
 
49