Spaces:
Paused
Paused
File size: 2,127 Bytes
ba292df | 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 53 54 55 56 57 | from typing import List
from fastapi import FastAPI, HTTPException, Response, Query, Request
from typing import Optional
from pydantic import BaseModel
from characterai import PyCAI
app = FastAPI()
@app.get("/")
def Another():
return {"Another": "Apa Lu Liat-liat?"}
@app.get("/AnotherAPI/{character}/chat/{api_key}/{message}")
async def chat_endpoint(character: str, api_key: str, message: str):
try:
if character == '2B':
client = PyCAI(api_key)
chat = client.chat.get_chat('csTC3hw0Fnj1Whnl0uV1Nb3_oYIillMQtdBH5NEl0Gs')
elif character == 'Narator':
client = PyCAI(api_key)
chat = client.chat.get_chat('oRrOSTDibssHQwoKEfNtwBwgBEFDr1aKfVPXjY1d8nA')
else:
return {"Another": "API Lu Mana?."}
participants = chat.get('participants', [])
if not participants:
return {"error": "Error"}
tgt = participants[1]['user']['username']
data = client.chat.send_message(chat['external_id'], tgt, message)
text = data['replies'][0]['text']
return {character.capitalize(): f"{text}"}
except Exception as e:
return {"error": f"Error: {e}"}
@app.get("/AnotherAPI/{character}/new_chat/{api_key}/{prompt}")
async def new_chat_endpoint(character: str, api_key: str, prompt: str):
try:
if character == '2B':
client = PyCAI(api_key)
chat = client.chat.new_chat('csTC3hw0Fnj1Whnl0uV1Nb3_oYIillMQtdBH5NEl0Gs')
elif character == 'Narator':
client = PyCAI(api_key)
chat = client.chat.new_chat('oRrOSTDibssHQwoKEfNtwBwgBEFDr1aKfVPXjY1d8nA')
else:
return {"Another": "API Lu Mana?."}
participants = chat.get('participants', [])
if not participants:
return {"error": "Error"}
tgt = participants[1]['user']['username']
data = client.chat.send_message(chat['external_id'], tgt, prompt)
text = data['replies'][0]['text']
return {character.capitalize(): f"{text}"}
except Exception as e:
return {"error": f"Error: {e}"}
|