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