Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,121 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
from
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
if __name__ == '__main__':
|
19 |
import uvicorn
|
20 |
-
uvicorn.run(app, host='0.0.0.0', port=8000)
|
|
|
1 |
+
import uvicorn
|
2 |
+
import requests
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from typing import List, Dict
|
6 |
+
import logging
|
7 |
+
|
8 |
+
# Configure logging
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
+
logger = logging.getLogger(__name__)
|
11 |
+
|
12 |
+
# API Configuration
|
13 |
+
TOKEN_URL = "https://spuckhogycrxcbomznwo.supabase.co/auth/v1/token?grant_type=refresh_token"
|
14 |
+
GPT_URL = "https://not-diamond-workers.t7-cc4.workers.dev/stream-message"
|
15 |
+
|
16 |
+
# Global token storage
|
17 |
+
ACCESS_TOKEN = "eyJhb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QiLCJhYWwiOiJhYWwxIiwiYW1yIjpbeyJtZXRob2QiOiJvYXV0aCIsInRpbWVzdGFtcCI6MTczMjM2MTAxNn1dLCJzZXNzaW9uX2lkIjoiZWZlNTRmMzgtYjFhYS00NjNmLThhODgtM2JiOWVjMTVmYTlkIiwiaXNfYW5vbnltb3VzIjpmYWxzZX0.fCdK8WF4ipmSucvRTtcs0-wgpzkeY6dIlKD62IjYS7M"
|
18 |
+
REFRESH_TOKEN = "VW2ol3sRN7S_-LqLtBE_fQ"
|
19 |
+
|
20 |
+
app = FastAPI(title="AI Response Server")
|
21 |
+
|
22 |
+
# Pydantic model for request validation
|
23 |
+
class MessageRequest(BaseModel):
|
24 |
+
messages: List[Dict[str, str]]
|
25 |
+
model: str = "chatgpt-4o-latest"
|
26 |
+
temperature: float = 0.9
|
27 |
+
|
28 |
+
# Token Refresh Function
|
29 |
+
def refresh_access_token(refresh_token: str):
|
30 |
+
global ACCESS_TOKEN
|
31 |
+
|
32 |
+
headers = {
|
33 |
+
"Authorization": f"Bearer {refresh_token}",
|
34 |
+
"Content-Type": "application/json"
|
35 |
+
}
|
36 |
+
|
37 |
+
payload = {"refresh_token": refresh_token}
|
38 |
+
|
39 |
+
try:
|
40 |
+
response = requests.post(TOKEN_URL, headers=headers, json=payload)
|
41 |
+
response.raise_for_status()
|
42 |
+
tokens = response.json()
|
43 |
+
|
44 |
+
ACCESS_TOKEN = tokens.get("access_token")
|
45 |
+
new_refresh_token = tokens.get("refresh_token")
|
46 |
+
|
47 |
+
logger.info("Access token refreshed successfully")
|
48 |
+
return ACCESS_TOKEN, new_refresh_token
|
49 |
+
|
50 |
+
except requests.exceptions.RequestException as e:
|
51 |
+
logger.error(f"Token refresh failed: {e}")
|
52 |
+
return None, None
|
53 |
+
|
54 |
+
# GPT Message Sending Function
|
55 |
+
def send_message_to_gpt(messages: List[Dict[str, str]], model: str = "chatgpt-4o-latest", temperature: float = 0.9):
|
56 |
+
global ACCESS_TOKEN
|
57 |
+
|
58 |
+
headers = {
|
59 |
+
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
60 |
+
"Content-Type": "application/json"
|
61 |
+
}
|
62 |
+
|
63 |
+
payload = {
|
64 |
+
"messages": messages,
|
65 |
+
"model": model,
|
66 |
+
"temperature": temperature
|
67 |
+
}
|
68 |
+
|
69 |
+
try:
|
70 |
+
response = requests.post(GPT_URL, headers=headers, json=payload)
|
71 |
+
response.raise_for_status()
|
72 |
+
|
73 |
+
return response.text
|
74 |
+
|
75 |
+
except requests.exceptions.HTTPError as http_err:
|
76 |
+
if response.status_code == 401: # Unauthorized, token might be expired
|
77 |
+
logger.warning("Access token expired. Attempting to refresh...")
|
78 |
+
new_access_token, _ = refresh_access_token(REFRESH_TOKEN)
|
79 |
+
|
80 |
+
if new_access_token:
|
81 |
+
# Retry the request with the new token
|
82 |
+
return send_message_to_gpt(messages, model, temperature)
|
83 |
+
|
84 |
+
logger.error(f"HTTP error occurred: {http_err}")
|
85 |
+
raise HTTPException(status_code=500, detail=f"GPT API request failed: {http_err}")
|
86 |
+
|
87 |
+
except requests.exceptions.RequestException as req_err:
|
88 |
+
logger.error(f"Request error: {req_err}")
|
89 |
+
raise HTTPException(status_code=500, detail=f"Request to GPT failed: {req_err}")
|
90 |
+
|
91 |
+
# Endpoint for GPT interaction
|
92 |
+
@app.post("/get-response")
|
93 |
+
async def get_gpt_response(request: MessageRequest):
|
94 |
+
try:
|
95 |
+
response = send_message_to_gpt(
|
96 |
+
messages=request.messages,
|
97 |
+
model=request.model,
|
98 |
+
temperature=request.temperature
|
99 |
+
)
|
100 |
+
return {"response": response}
|
101 |
+
except HTTPException as e:
|
102 |
+
raise e
|
103 |
+
|
104 |
+
# Health check endpoint
|
105 |
+
@app.get("/health")
|
106 |
+
async def health_check():
|
107 |
+
return {"status": "healthy"}
|
108 |
+
|
109 |
+
# DNS Resolution Test Endpoint
|
110 |
+
@app.get("/test-dns")
|
111 |
+
async def test_dns():
|
112 |
+
try:
|
113 |
+
import socket
|
114 |
+
socket.gethostbyname('not-diamond-workers.t7-cc4.workers.dev')
|
115 |
+
return {"status": "success", "message": "DNS resolution successful"}
|
116 |
+
except Exception as e:
|
117 |
+
return {"status": "failure", "error": str(e)}
|
118 |
+
|
119 |
if __name__ == '__main__':
|
120 |
import uvicorn
|
121 |
+
uvicorn.run(app, host='0.0.0.0', port=8000)
|