Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,16 @@
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
-
from
|
4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
-
#
|
9 |
app.add_middleware(
|
10 |
CORSMiddleware,
|
11 |
allow_origins=["*"],
|
@@ -14,14 +19,122 @@ app.add_middleware(
|
|
14 |
allow_headers=["*"],
|
15 |
)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
async def execute_workflow(request: Request):
|
23 |
-
workflow = await request.json()
|
24 |
-
print(workflow)
|
25 |
-
data = execute(workflow)
|
26 |
-
return data
|
27 |
|
|
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from fastapi.responses import HTMLResponse, StreamingResponse
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from typing import List, Optional, Union
|
6 |
+
import requests
|
7 |
+
import time
|
8 |
+
from fastapi.templating import Jinja2Templates
|
9 |
+
|
10 |
|
11 |
app = FastAPI()
|
12 |
|
13 |
+
# CORS
|
14 |
app.add_middleware(
|
15 |
CORSMiddleware,
|
16 |
allow_origins=["*"],
|
|
|
19 |
allow_headers=["*"],
|
20 |
)
|
21 |
|
22 |
+
templates = Jinja2Templates(directory="templates")
|
23 |
+
|
24 |
+
|
25 |
+
class MessageContent(BaseModel):
|
26 |
+
type: str
|
27 |
+
text: str
|
28 |
+
|
29 |
+
|
30 |
+
class ChatMessage(BaseModel):
|
31 |
+
role: str
|
32 |
+
content: Union[str, List[MessageContent]]
|
33 |
+
|
34 |
+
|
35 |
+
class ChatRequest(BaseModel):
|
36 |
+
message: str
|
37 |
+
messages: List[ChatMessage]
|
38 |
+
model: Optional[str] = "meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8"
|
39 |
+
|
40 |
+
|
41 |
+
cookies = {
|
42 |
+
'intercom-id-evnv2y8k': 'fea4d452-f9be-42e0-93e3-1e47a3836362',
|
43 |
+
'intercom-device-id-evnv2y8k': '2bb3e469-0159-4b6b-a33e-1aea4b51ccb1',
|
44 |
+
'__stripe_mid': 'e0f7c1ba-56c6-44d4-ba1d-cf4611453eb43cf922',
|
45 |
+
'state-csrf': '6f2o8nqgee2dfqdmhaxipe',
|
46 |
+
'together_auth_cookie': '%7B%22expires%22%3A%222026-04-09T15%3A14%3A08.985Z%22%2C%22session%22%3A%220eae08c6fd1b79a22476a317d440a2104d74cd3ba333e40771b5ce50a90784297eb82eff36263debca2ee0658abe3e43cab97f87794421111d4bdec56b43dd2595ee22a165c123ba3d0f807759555b5f6d3f51b7c248e7cefcdf0f0b897f62b25b2a569e2cb89633032f15dca9818f39ed49f3ac2d7e0bc3d24517c62c78b1e4%22%7D',
|
47 |
+
'__stripe_sid': '979e00a2-06ed-45be-9a95-88d7e7580f625ccce4',
|
48 |
+
'intercom-session-evnv2y8k': 'TzZzSzBNRG8xdHJtTVprMm1zUXFob0M2ekhFV3VmeDZFcW5UVldlYmFYc3RsRjFmdWJidjU1ZXVSZzNOSW9QTE82OUx6anlvMWVncmlTd2ZvOERDUXN4OUdoSEM5ZzRnQmh4d2o5S3JKeDA9LS00S3JOclNpNzU0VkVBaTNRNWhSMm93PT0=--2719775e99e920753d35527a45a6731bac5e8f8f',
|
49 |
+
'AMP_7112ee0414': 'JTdCJTIyZGV2aWNlSWQlMjIlM0ElMjJmY2ZmNjE3Ny00Yzg0LTRlOTItYTFhMC1kM2Y1ZjllOTFkYTglMjIlMkMlMjJ1c2VySWQlMjIlM0ElMjI2N2I1ZDkwNDNkZTIyN2Q0OGIzMWEwZTMlMjIlMkMlMjJzZXNzaW9uSWQlMjIlM0ExNzQ0MjExNjQyMjEwJTJDJTIyb3B0T3V0JTIyJTNBZmFsc2UlMkMlMjJsYXN0RXZlbnRUaW1lJTIyJTNBMTc0NDIxMTc1ODAwOSUyQyUyMmxhc3RFdmVudElkJTIyJTNBMjMyJTJDJTIycGFnZUNvdW50ZXIlMjIlM0E1JTdE',
|
50 |
+
}
|
51 |
+
|
52 |
+
headers = {
|
53 |
+
'accept': 'application/json',
|
54 |
+
'accept-language': 'en-US,en;q=0.9,ja;q=0.8',
|
55 |
+
'authorization': 'Bearer 4d900964e385651ea685af6f6cd5573a17b421f50657f73f903525177915a7e2',
|
56 |
+
'content-type': 'application/json',
|
57 |
+
'priority': 'u=1, i',
|
58 |
+
'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
|
59 |
+
'sec-ch-ua-mobile': '?0',
|
60 |
+
'sec-ch-ua-platform': '"macOS"',
|
61 |
+
'sec-fetch-dest': 'empty',
|
62 |
+
'sec-fetch-mode': 'cors',
|
63 |
+
'sec-fetch-site': 'same-origin',
|
64 |
+
'x-stainless-arch': 'unknown',
|
65 |
+
'x-stainless-lang': 'js',
|
66 |
+
'x-stainless-os': 'Unknown',
|
67 |
+
'x-stainless-package-version': '0.11.1',
|
68 |
+
'x-stainless-retry-count': '0',
|
69 |
+
'x-stainless-runtime': 'browser:chrome',
|
70 |
+
'x-stainless-runtime-version': '135.0.0',
|
71 |
+
'referer': 'https://api.together.ai/playground/v2/chat/meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8',
|
72 |
+
}
|
73 |
+
|
74 |
+
|
75 |
+
@app.get("/", response_class=HTMLResponse)
|
76 |
+
async def index(request: Request):
|
77 |
+
return {"status":"running"}
|
78 |
+
|
79 |
+
|
80 |
+
@app.post("/chat")
|
81 |
+
async def chat(request_data: ChatRequest):
|
82 |
+
user_input = request_data.message
|
83 |
+
messages = request_data.messages
|
84 |
+
model = request_data.model
|
85 |
+
|
86 |
+
current_messages = messages.copy()
|
87 |
+
|
88 |
+
if messages and isinstance(messages[-1].content, list):
|
89 |
+
current_messages = messages
|
90 |
+
else:
|
91 |
+
current_messages.append(ChatMessage(
|
92 |
+
role="user",
|
93 |
+
content=[MessageContent(type="text", text=user_input)]
|
94 |
+
))
|
95 |
+
|
96 |
+
json_data = {
|
97 |
+
'model': model,
|
98 |
+
'max_tokens': None,
|
99 |
+
'temperature': 0.7,
|
100 |
+
'top_p': 0.7,
|
101 |
+
'top_k': 50,
|
102 |
+
'repetition_penalty': 1,
|
103 |
+
'stream_tokens': True,
|
104 |
+
'stop': ['<|eot_id|>', '<|eom_id|>'],
|
105 |
+
'messages': [m.dict() for m in current_messages],
|
106 |
+
'stream': True,
|
107 |
+
}
|
108 |
+
|
109 |
+
def generate():
|
110 |
+
max_retries = 5
|
111 |
+
|
112 |
+
for attempt in range(max_retries):
|
113 |
+
response = requests.post(
|
114 |
+
'https://api.together.ai/inference',
|
115 |
+
cookies=cookies,
|
116 |
+
headers=headers,
|
117 |
+
json=json_data,
|
118 |
+
stream=True
|
119 |
+
)
|
120 |
+
|
121 |
+
if response.status_code == 200:
|
122 |
+
for line in response.iter_lines():
|
123 |
+
if line:
|
124 |
+
decoded_line = line.decode('utf-8')
|
125 |
+
if decoded_line.startswith("data: "):
|
126 |
+
yield f"{decoded_line}\n\n"
|
127 |
+
return
|
128 |
+
elif response.status_code == 429:
|
129 |
+
if attempt < max_retries - 1:
|
130 |
+
time.sleep(0.5)
|
131 |
+
continue
|
132 |
+
yield 'data: {"error": "Rate limited, maximum retries reached"}\n\n'
|
133 |
+
return
|
134 |
+
else:
|
135 |
+
yield f'data: {{"error": "Unexpected status code: {response.status_code}"}}\n\n'
|
136 |
+
return
|
137 |
|
138 |
+
yield 'data: {"error": "Maximum retries reached"}\n\n'
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
+
return StreamingResponse(generate(), media_type="text/event-stream")
|