Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,47 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
-
import
|
| 3 |
import os
|
| 4 |
-
import json
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@app.route('/', methods=['GET'])
|
| 12 |
def generate_text():
|
| 13 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Get the user's prompt from query parameters
|
| 15 |
user_prompt = request.args.get('prompt', 'What is the capital of France?')
|
| 16 |
|
| 17 |
-
# Prepare the
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"
|
| 22 |
-
"temperature": 0.7,
|
| 23 |
-
"top_p": 0.95,
|
| 24 |
-
"return_full_text": True
|
| 25 |
}
|
| 26 |
-
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
| 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':
|
| 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"
|
| 64 |
-
return jsonify({'error': f'An
|
| 65 |
|
| 66 |
if __name__ == '__main__':
|
| 67 |
app.run(debug=True)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
# Initialize the InferenceClient
|
| 8 |
+
client = InferenceClient(
|
| 9 |
+
token=os.getenv('HUGGING_FACE_API_KEY') # Make sure to set this environment variable
|
| 10 |
+
)
|
| 11 |
|
| 12 |
@app.route('/', methods=['GET'])
|
| 13 |
def generate_text():
|
| 14 |
try:
|
| 15 |
+
# Verify API key is set
|
| 16 |
+
if not os.getenv('HUGGING_FACE_API_KEY'):
|
| 17 |
+
return jsonify({'error': 'HUGGING_FACE_API_KEY environment variable is not set'}), 500
|
| 18 |
+
|
| 19 |
# Get the user's prompt from query parameters
|
| 20 |
user_prompt = request.args.get('prompt', 'What is the capital of France?')
|
| 21 |
|
| 22 |
+
# Prepare the messages
|
| 23 |
+
messages = [
|
| 24 |
+
{
|
| 25 |
+
"role": "user",
|
| 26 |
+
"content": user_prompt
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
+
]
|
| 29 |
|
| 30 |
+
# Generate completion
|
| 31 |
+
completion = client.chat.completions.create(
|
| 32 |
+
model="deepseek-ai/DeepSeek-R1",
|
| 33 |
+
messages=messages,
|
| 34 |
+
max_tokens=500
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Extract the response
|
| 38 |
+
response_text = completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
return jsonify({'response': response_text})
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
+
print(f"Error occurred: {str(e)}")
|
| 44 |
+
return jsonify({'error': f'An error occurred: {str(e)}'}), 500
|
| 45 |
|
| 46 |
if __name__ == '__main__':
|
| 47 |
app.run(debug=True)
|