File size: 1,619 Bytes
42b9643 b72d25f 42b9643 b72d25f 42b9643 b72d25f 42b9643 b72d25f |
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 48 49 50 51 52 |
from flask import Flask, request, jsonify
import asyncio
import aiohttp, os
app = Flask(__name__)
async def claude_new_process(prompt):
headers = {
"x-api-key": os.environ.get('API_KEY'),
"content-type": "application/json"
}
data = {
"prompt": prompt,
"model": "claude-v1.3-100k",
"max_tokens_to_sample": 1000000,
"temperature": 0.52,
"stopsequences": "\n\nHuman: ",
}
async with aiohttp.ClientSession() as session:
async with session.post("https://api.anthropic.com/v1/complete", json=data, headers=headers) as response:
if response.status == 200:
return 200, await response.json()
else:
return response.status, "error"
@app.route('/api/claude', methods=['POST'])
def api_claude_new_process():
prompt = request.json['prompt']
key = request.json['password']
print(f"{prompt} {key}")
if key != os.environ.get('PASSWORD'):
return jsonify({'error': 'wrong password'}), 403
print(f"Called with prompts: {prompt}")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
status_code, response = loop.run_until_complete(claude_new_process(prompt))
if status_code == 200:
print(response)
return response, 200
else:
return jsonify({'error': 'error'}), status_code
async def test():
status, response = await claude_new_process('\n\nHuman: Hello! \n\nGigachad: ')
print(response['completion'])
if __name__ == '__main__':
#asyncio.run(test())
app.run(debug=True, port=7860, host='0.0.0.0')
|