Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import requests
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
|
@@ -19,22 +20,48 @@ def generate_text():
|
|
| 19 |
"parameters": {
|
| 20 |
"max_new_tokens": 500,
|
| 21 |
"temperature": 0.7,
|
| 22 |
-
"top_p": 0.95
|
|
|
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
# Make request to Hugging Face API
|
| 27 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 28 |
-
response.raise_for_status() # Raise an exception for bad status codes
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
return jsonify({'response': generated_text})
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
-
print(f"
|
| 37 |
-
return jsonify({'error': 'An unexpected error occurred
|
| 38 |
|
| 39 |
if __name__ == '__main__':
|
| 40 |
app.run(debug=True)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
|
|
|
| 20 |
"parameters": {
|
| 21 |
"max_new_tokens": 500,
|
| 22 |
"temperature": 0.7,
|
| 23 |
+
"top_p": 0.95,
|
| 24 |
+
"return_full_text": True
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
| 28 |
# Make request to Hugging Face API
|
| 29 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
|
|
|
| 30 |
|
| 31 |
+
# Print response for debugging
|
| 32 |
+
print(f"Status Code: {response.status_code}")
|
| 33 |
+
print(f"Response Content: {response.text}")
|
| 34 |
+
|
| 35 |
+
# Check if the model is still loading
|
| 36 |
+
if response.status_code == 503:
|
| 37 |
+
return jsonify({'error': 'Model is loading, please try again in a few seconds'}), 503
|
| 38 |
+
|
| 39 |
+
# Check for other error status codes
|
| 40 |
+
response.raise_for_status()
|
| 41 |
+
|
| 42 |
+
# Parse the response
|
| 43 |
+
response_data = response.json()
|
| 44 |
+
|
| 45 |
+
# Handle different response formats
|
| 46 |
+
if isinstance(response_data, list) and len(response_data) > 0:
|
| 47 |
+
if 'generated_text' in response_data[0]:
|
| 48 |
+
generated_text = response_data[0]['generated_text']
|
| 49 |
+
else:
|
| 50 |
+
generated_text = response_data[0]
|
| 51 |
+
else:
|
| 52 |
+
generated_text = response_data
|
| 53 |
|
| 54 |
return jsonify({'response': generated_text})
|
| 55 |
|
| 56 |
+
except requests.exceptions.RequestException as e:
|
| 57 |
+
print(f"API Request Error: {str(e)}")
|
| 58 |
+
return jsonify({'error': f'API Request Error: {str(e)}'}), 500
|
| 59 |
+
except json.JSONDecodeError as e:
|
| 60 |
+
print(f"JSON Decode Error: {str(e)}")
|
| 61 |
+
return jsonify({'error': 'Invalid response from API'}), 500
|
| 62 |
except Exception as e:
|
| 63 |
+
print(f"Unexpected Error: {str(e)}")
|
| 64 |
+
return jsonify({'error': f'An unexpected error occurred: {str(e)}'}), 500
|
| 65 |
|
| 66 |
if __name__ == '__main__':
|
| 67 |
app.run(debug=True)
|