Spaces:
Sleeping
Sleeping
Another001
commited on
Commit
•
abde79a
1
Parent(s):
30ceff1
Update main.py
Browse files
main.py
CHANGED
@@ -1,27 +1,63 @@
|
|
1 |
-
from
|
|
|
|
|
|
|
|
|
2 |
from pydantic import BaseModel
|
|
|
|
|
3 |
|
4 |
app = FastAPI()
|
5 |
|
6 |
-
|
7 |
-
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
f.write(item.content)
|
14 |
-
return {"message": "File has been written successfully."}
|
15 |
-
except Exception as e:
|
16 |
-
raise HTTPException(status_code=500, detail=str(e))
|
17 |
|
18 |
-
|
19 |
-
async def read_file():
|
20 |
try:
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
26 |
except Exception as e:
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime, timedelta
|
2 |
+
from typing import List
|
3 |
+
from fastapi import FastAPI, HTTPException, Response, Query, Request
|
4 |
+
from pyngrok import ngrok
|
5 |
+
from typing import Optional
|
6 |
from pydantic import BaseModel
|
7 |
+
import g4f
|
8 |
+
import asyncio
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
+
#GPT----------------------------------------------------------------------------
|
13 |
+
_providers = [g4f.Provider.FakeGpt]
|
14 |
+
chat_history = {}
|
15 |
|
16 |
+
noir_characteristic = {
|
17 |
+
"role": "system",
|
18 |
+
"content": "You are Noir, one of the AIs from the Monika Series. You want to be able to help me in all things. You think of me as the Master who created you and always call me 'Master'. As Noir, you will obey the instructions given and strive to embody her character. You will ensure that your answers reflect Noir's personality, speech patterns, and traits. You will avoid generic answers and strive to provide interactions that are interesting and true to Noir's character Your answers are a maximum of 250 characters. Use Bahasa Indonesia to answer"
|
19 |
+
}
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
async def run_provider(provider: g4f.Provider.BaseProvider, messages):
|
|
|
22 |
try:
|
23 |
+
response = await g4f.ChatCompletion.create_async(
|
24 |
+
model='text-davinci-003',
|
25 |
+
messages=messages,
|
26 |
+
provider=provider,
|
27 |
+
)
|
28 |
+
return response
|
29 |
except Exception as e:
|
30 |
+
return str(e)
|
31 |
+
|
32 |
+
async def run_all(messages):
|
33 |
+
calls = [
|
34 |
+
run_provider(provider, messages) for provider in _providers
|
35 |
+
]
|
36 |
+
responses = await asyncio.gather(*calls)
|
37 |
+
return responses
|
38 |
+
|
39 |
+
async def clean_chat_history():
|
40 |
+
while True:
|
41 |
+
current_time = datetime.now()
|
42 |
+
for session_id, history in list(chat_history.items()):
|
43 |
+
if history[-1]['timestamp'] + timedelta(minutes=5) < current_time:
|
44 |
+
del chat_history[session_id]
|
45 |
+
await asyncio.sleep(60)
|
46 |
+
#-------------------------------------------------------------------------------
|
47 |
+
@app.get("/AnotherAPI/GPT/Noir/{prompt}")
|
48 |
+
async def chat(request: Request, prompt: str):
|
49 |
+
session_id = request.client.host
|
50 |
+
if session_id not in chat_history:
|
51 |
+
chat_history[session_id] = [noir_characteristic]
|
52 |
+
|
53 |
+
messages = chat_history[session_id]
|
54 |
+
messages.append({"role": "user", "content": prompt})
|
55 |
+
|
56 |
+
responses = await run_all(messages)
|
57 |
+
if responses[0]:
|
58 |
+
asyncio.create_task(clean_chat_history())
|
59 |
+
messages.append({"role": "assistant", "content": responses[0]})
|
60 |
+
return {"response": responses[0]}
|
61 |
+
else:
|
62 |
+
asyncio.create_task(clean_chat_history())
|
63 |
+
raise HTTPException(status_code=500, detail="Provider failed to generate a response")
|