Team Ai
Apppublic

Myaptla/claudeAPI

sourceHugging Faceunknownupdated 3y agoView on Hugging Face
0likes
webapi_claude.py52 linesDownload Raw Back to root
1from flask import Flask, request, jsonify2import asyncio3import aiohttp, os4 5app = Flask(__name__)6 7async def claude_new_process(prompt):8    headers = {9        "x-api-key": os.environ.get('API_KEY'),10        "content-type": "application/json"11    }12    data = {13        "prompt": prompt,14        "model": "claude-v1.3-100k",15        "max_tokens_to_sample": 1000000,16        "temperature": 0.52,17        "stopsequences": "\n\nHuman: ",18    }19 20    async with aiohttp.ClientSession() as session:21        async with session.post("https://api.anthropic.com/v1/complete", json=data, headers=headers) as response:22            if response.status == 200:23                return 200, await response.json()24            else:25                return response.status, "error"26 27@app.route('/api/claude', methods=['POST'])28def api_claude_new_process():29    prompt = request.json['prompt']30    key = request.json['password']31    print(f"{prompt} {key}")32    if key != os.environ.get('PASSWORD'):33        return jsonify({'error': 'wrong password'}), 40334 35    print(f"Called with prompts: {prompt}")36    loop = asyncio.new_event_loop()37    asyncio.set_event_loop(loop)38    status_code, response = loop.run_until_complete(claude_new_process(prompt))39    if status_code == 200:40        print(response)41        return response, 20042    else:43        return jsonify({'error': 'error'}), status_code44 45async def test():46    status, response = await claude_new_process('\n\nHuman: Hello! \n\nGigachad: ')47    print(response['completion'])48 49if __name__ == '__main__':50    #asyncio.run(test())51    app.run(debug=True, port=7860, host='0.0.0.0')52