chipling commited on
Commit
af5e18d
·
verified ·
1 Parent(s): e700505

Upload 17 files

Browse files
__pycache__/app.cpython-312.pyc CHANGED
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
 
app.py CHANGED
@@ -1,9 +1,10 @@
1
  from fastapi import FastAPI, Request
2
  from fastapi.responses import StreamingResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
- from models.together.main import TogetherAPI
5
- from models.vercel.main import XaiAPI, GroqAPI
6
-
 
7
 
8
  app = FastAPI()
9
 
@@ -17,7 +18,7 @@ app.add_middleware(
17
 
18
  @app.get("/")
19
  async def root():
20
- return {"message": "Server Running Successfully"}
21
 
22
  @app.post("/api/v1/generate")
23
  async def generate(request: Request):
@@ -45,6 +46,7 @@ async def generate(request: Request):
45
  together_models = TogetherAPI().get_model_list()
46
  xai_models = XaiAPI().get_model_list()
47
  groq_models = GroqAPI().get_model_list()
 
48
 
49
  if model in together_models:
50
  streamModel = TogetherAPI()
@@ -52,6 +54,8 @@ async def generate(request: Request):
52
  streamModel = XaiAPI()
53
  elif model in groq_models:
54
  streamModel = GroqAPI()
 
 
55
  else:
56
  return {"error": f"Model '{model}' is not supported."}
57
 
@@ -68,5 +72,33 @@ async def get_models():
68
  streamModel = TogetherAPI()
69
  models = streamModel.get_model_list()
70
  return {"models": models}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  except Exception as e:
72
  return {"error": f"An error occurred: {str(e)}"}
 
1
  from fastapi import FastAPI, Request
2
  from fastapi.responses import StreamingResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
+ from models.text.together.main import TogetherAPI
5
+ from models.text.vercel.main import XaiAPI, GroqAPI, DeepinfraAPI
6
+ from models.image.vercel.main import FalAPI
7
+ from models.image.together.main import TogetherImageAPI
8
 
9
  app = FastAPI()
10
 
 
18
 
19
  @app.get("/")
20
  async def root():
21
+ return {"status":"ok", "routes":{"/":"GET", "/api/v1/generate":"POST", "/api/v1/models":"GET", "/api/v1/generate-images":"POST"}, "models": ["text", "image"]}
22
 
23
  @app.post("/api/v1/generate")
24
  async def generate(request: Request):
 
46
  together_models = TogetherAPI().get_model_list()
47
  xai_models = XaiAPI().get_model_list()
48
  groq_models = GroqAPI().get_model_list()
49
+ deepinfra_models = DeepinfraAPI().get_model_list()
50
 
51
  if model in together_models:
52
  streamModel = TogetherAPI()
 
54
  streamModel = XaiAPI()
55
  elif model in groq_models:
56
  streamModel = GroqAPI()
57
+ elif model in deepinfra_models:
58
+ streamModel = DeepinfraAPI()
59
  else:
60
  return {"error": f"Model '{model}' is not supported."}
61
 
 
72
  streamModel = TogetherAPI()
73
  models = streamModel.get_model_list()
74
  return {"models": models}
75
+ except Exception as e:
76
+ return {"error": f"An error occurred: {str(e)}"}
77
+
78
+ @app.post('/api/v1/generate-images')
79
+ async def generate_images(request: Request):
80
+ data = await request.json()
81
+ prompt = data['prompt']
82
+ model = data['model']
83
+ print(model)
84
+
85
+ fal_models = FalAPI().get_model_list()
86
+ together_models = TogetherImageAPI().get_model_list()
87
+ if not prompt or not model:
88
+ return {"error": "Invalid request. 'prompt' and 'model' are required."}
89
+ if model in fal_models:
90
+ streamModel = FalAPI()
91
+ elif model in together_models:
92
+ streamModel = TogetherImageAPI()
93
+ else:
94
+ return {"error": f"Model '{model}' is not supported."}
95
+ try:
96
+ query = {
97
+ 'prompt': prompt,
98
+ 'modelId': model,
99
+ }
100
+ response = await streamModel.generate(query)
101
+ return response
102
+
103
  except Exception as e:
104
  return {"error": f"An error occurred: {str(e)}"}
models/.DS_Store CHANGED
Binary files a/models/.DS_Store and b/models/.DS_Store differ
 
models/image/together/__pycache__/main.cpython-312.pyc ADDED
Binary file (2.76 kB). View file
 
models/image/together/main.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpx
2
+
3
+ class TogetherImageAPI:
4
+ headers = {
5
+ 'sec-ch-ua-platform': '"macOS"',
6
+ 'Authorization': 'Bearer 869f5ecd80fc6482ccb99f37179cb2c162879925224ae9d91caffc35d9b534b3',
7
+ 'Referer': 'https://api.together.ai/playground/image/black-forest-labs/FLUX.1-dev',
8
+ 'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
9
+ 'sec-ch-ua-mobile': '?0',
10
+ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
11
+ 'Accept': 'application/json, text/plain, */*',
12
+ 'Content-Type': 'application/json',
13
+ }
14
+
15
+ def __init__(self):
16
+ self.name = "TogetherImageAPI"
17
+ self.url = "https://api.together.ai/inferences"
18
+
19
+ def get_model_list(self):
20
+ models = [
21
+ "black-forest-labs/FLUX.1-dev",
22
+ ]
23
+ return models
24
+
25
+ async def generate(self, json_data):
26
+ print(json_data)
27
+ model = json_data['modelId']
28
+ prompt = json_data['prompt']
29
+ negative_prompt = json_data.get('negative_prompt', '')
30
+ width = json_data.get('width', 1024)
31
+ height = json_data.get('height', 768)
32
+ steps = json_data.get('steps', 28)
33
+
34
+ data = {
35
+ 'model': model,
36
+ 'prompt': prompt,
37
+ 'negative_prompt': negative_prompt,
38
+ 'width': width,
39
+ 'height': height,
40
+ 'steps': steps,
41
+ 'n': 1,
42
+ 'response_format': 'b64_json',
43
+ 'stop': [],
44
+ }
45
+
46
+ async with httpx.AsyncClient() as client:
47
+ response = await client.post("https://api.together.ai/inference", json=json_data)
48
+ if response.status_code == 200:
49
+ return response.json()
50
+ else:
51
+ raise Exception(f"Error: {response.status_code} - {response.text}")
52
+
models/image/vercel/__pycache__/main.cpython-312.pyc ADDED
Binary file (2.41 kB). View file
 
models/image/vercel/main.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpx
2
+
3
+ class FalAPI:
4
+ headers = {
5
+ 'accept': '*/*',
6
+ 'accept-language': 'en-US,en;q=0.9,ja;q=0.8',
7
+ 'content-type': 'application/json',
8
+ 'origin': 'https://fal-image-generator.vercel.app',
9
+ 'priority': 'u=1, i',
10
+ 'referer': 'https://fal-image-generator.vercel.app/',
11
+ 'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
12
+ 'sec-ch-ua-mobile': '?0',
13
+ 'sec-ch-ua-platform': '"macOS"',
14
+ 'sec-fetch-dest': 'empty',
15
+ 'sec-fetch-mode': 'cors',
16
+ 'sec-fetch-site': 'same-origin',
17
+ 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
18
+ }
19
+ def __init__(self):
20
+ self.name = "FalAPI"
21
+ self.url = "https://fal-image-generator.vercel.app/api/generate-images"
22
+
23
+ def get_model_list(self):
24
+ models = [
25
+ "fal-ai/fast-sdxl",
26
+ "fal-ai/flux/dev"
27
+ "fal-ai/flux-pro/v1.1-ultra"
28
+ "fal-ai/ideogram/v2"
29
+ "fal-ai/recraft-v3"
30
+ "fal-ai/hyper-sdxl"
31
+ ]
32
+ return models
33
+
34
+ async def generate(self, json_data):
35
+ json_data.update({'provider': 'fal'})
36
+
37
+ async with httpx.AsyncClient() as client:
38
+ response = await client.post(self.url, json=json_data)
39
+ if response.status_code == 200:
40
+ return response.json()
41
+ else:
42
+ raise Exception(f"Error: {response.status_code} - {response.text}")
models/text/.DS_Store ADDED
Binary file (6.15 kB). View file
 
models/text/together/__pycache__/main.cpython-312.pyc ADDED
Binary file (5.81 kB). View file
 
models/text/together/main.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpx
2
+ import asyncio
3
+
4
+ class TogetherAPI:
5
+
6
+ cookies = {
7
+ 'intercom-id-evnv2y8k': 'fea4d452-f9be-42e0-93e3-1e47a3836362',
8
+ 'intercom-device-id-evnv2y8k': '2bb3e469-0159-4b6b-a33e-1aea4b51ccb1',
9
+ '__stripe_mid': 'e0f7c1ba-56c6-44d4-ba1d-cf4611453eb43cf922',
10
+ 'state-csrf': '6f2o8nqgee2dfqdmhaxipe',
11
+ 'together_auth_cookie': '%7B%22expires%22%3A%222026-04-09T15%3A14%3A08.985Z%22%2C%22session%22%3A%220eae08c6fd1b79a22476a317d440a2104d74cd3ba333e40771b5ce50a90784297eb82eff36263debca2ee0658abe3e43cab97f87794421111d4bdec56b43dd2595ee22a165c123ba3d0f807759555b5f6d3f51b7c248e7cefcdf0f0b897f62b25b2a569e2cb89633032f15dca9818f39ed49f3ac2d7e0bc3d24517c62c78b1e4%22%7D',
12
+ '__stripe_sid': '979e00a2-06ed-45be-9a95-88d7e7580f625ccce4',
13
+ 'intercom-session-evnv2y8k': 'TzZzSzBNRG8xdHJtTVprMm1zUXFob0M2ekhFV3VmeDZFcW5UVldlYmFYc3RsRjFmdWJidjU1ZXVSZzNOSW9QTE82OUx6anlvMWVncmlTd2ZvOERDUXN4OUdoSEM5ZzRnQmh4d2o5S3JKeDA9LS00S3JOclNpNzU0VkVBaTNRNWhSMm93PT0=--2719775e99e920753d35527a45a6731bac5e8f8f',
14
+ 'AMP_7112ee0414': 'JTdCJTIyZGV2aWNlSWQlMjIlM0ElMjJmY2ZmNjE3Ny00Yzg0LTRlOTItYTFhMC1kM2Y1ZjllOTFkYTglMjIlMkMlMjJ1c2VySWQlMjIlM0ElMjI2N2I1ZDkwNDNkZTIyN2Q0OGIzMWEwZTMlMjIlMkMlMjJzZXNzaW9uSWQlMjIlM0ExNzQ0MjExNjQyMjEwJTJDJTIyb3B0T3V0JTIyJTNBZmFsc2UlMkMlMjJsYXN0RXZlbnRUaW1lJTIyJTNBMTc0NDIxMTc1ODAwOSUyQyUyMmxhc3RFdmVudElkJTIyJTNBMjMyJTJDJTIycGFnZUNvdW50ZXIlMjIlM0E1JTdE',
15
+ }
16
+
17
+ headers = {
18
+ 'accept': 'application/json',
19
+ 'accept-language': 'en-US,en;q=0.9,ja;q=0.8',
20
+ 'authorization': 'Bearer 4d900964e385651ea685af6f6cd5573a17b421f50657f73f903525177915a7e2',
21
+ 'content-type': 'application/json',
22
+ 'priority': 'u=1, i',
23
+ 'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
24
+ 'sec-ch-ua-mobile': '?0',
25
+ 'sec-ch-ua-platform': '"macOS"',
26
+ 'sec-fetch-dest': 'empty',
27
+ 'sec-fetch-mode': 'cors',
28
+ 'sec-fetch-site': 'same-origin',
29
+ 'x-stainless-arch': 'unknown',
30
+ 'x-stainless-lang': 'js',
31
+ 'x-stainless-os': 'Unknown',
32
+ 'x-stainless-package-version': '0.11.1',
33
+ 'x-stainless-retry-count': '0',
34
+ 'x-stainless-runtime': 'browser:chrome',
35
+ 'x-stainless-runtime-version': '135.0.0',
36
+ 'referer': 'https://api.together.ai/playground/v2/chat/meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8',
37
+ }
38
+
39
+ def __init__(self):
40
+ self.base_url = "https://api.together.ai/inference"
41
+
42
+ def get_model_list(self):
43
+ models = ['meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8', 'meta-llama/Llama-4-Scout-17B-16E-Instruct', 'deepseek-ai/DeepSeek-R1', 'deepseek-ai/DeepSeek-V3', 'Qwen/Qwen2.5-VL-72B-Instruct', 'google/gemma-2-27b-it']
44
+ return models
45
+
46
+ async def generate(self, json_data: dict):
47
+ max_retries = 5
48
+ for attempt in range(max_retries):
49
+ async with httpx.AsyncClient(timeout=None) as client:
50
+ try:
51
+ request_ctx = client.stream(
52
+ "POST",
53
+ "https://api.together.ai/inference",
54
+ cookies=TogetherAPI.cookies,
55
+ headers=TogetherAPI.headers,
56
+ json=json_data
57
+ )
58
+
59
+ async with request_ctx as response:
60
+ if response.status_code == 200:
61
+ async for line in response.aiter_lines():
62
+ if line:
63
+ yield f"{line}\n"
64
+ return
65
+ elif response.status_code == 429:
66
+ if attempt < max_retries - 1:
67
+ await asyncio.sleep(0.5)
68
+ continue
69
+ yield "data: [Rate limited, max retries]\n\n"
70
+ return
71
+ else:
72
+ yield f"data: [Unexpected status code: {response.status_code}]\n\n"
73
+ return
74
+ except Exception as e:
75
+ yield f"data: [Connection error: {str(e)}]\n\n"
76
+ return
77
+
78
+ yield "data: [Max retries reached]\n\n"
79
+
models/text/vercel/.DS_Store ADDED
Binary file (6.15 kB). View file
 
models/text/vercel/__pycache__/main.cpython-312.pyc ADDED
Binary file (13.4 kB). View file
 
models/text/vercel/main.py ADDED
@@ -0,0 +1,301 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpx
2
+ import asyncio
3
+ import random
4
+ import json
5
+
6
+ class XaiAPI:
7
+
8
+ headers = {
9
+ 'accept': '*/*',
10
+ 'accept-language': 'en-US,en;q=0.9,ja;q=0.8',
11
+ 'content-type': 'application/json',
12
+ 'origin': 'https://ai-sdk-starter-xai.vercel.app',
13
+ 'referer': 'https://ai-sdk-starter-xai.vercel.app/',
14
+ 'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
15
+ 'sec-ch-ua-mobile': '?0',
16
+ 'sec-ch-ua-platform': '"macOS"',
17
+ 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36'
18
+ }
19
+
20
+
21
+ def __init__(self):
22
+ self.base_url = "https://ai-sdk-starter-xai.vercel.app/api/chat"
23
+
24
+ def get_model_list(self):
25
+ models = ["grok-3-mini", "grok-2-1212", "grok-3", "grok-3-fast", "grok-3-mini-fast"]
26
+ return models
27
+
28
+ def convert(messages):
29
+ converted = []
30
+ for message in messages:
31
+ role = message.get("role", "user")
32
+ content = message.get("content", "")
33
+
34
+ if isinstance(content, list):
35
+ parts = content
36
+ text_content = "\n".join([p.get("text", "") for p in content if p.get("type") == "text"])
37
+ else:
38
+ text_content = str(content)
39
+ parts = [{"type": "text", "text": text_content}]
40
+ if role == "assistant":
41
+ parts.insert(0, {"type": "step-start"})
42
+
43
+ converted.append({
44
+ "role": role,
45
+ "content": text_content,
46
+ "parts": parts
47
+ })
48
+ return converted
49
+
50
+ async def generate(self, json_data: dict):
51
+ messages = XaiAPI.convert(json_data["messages"])
52
+
53
+ request_data = {
54
+ "id": "".join(random.choices("0123456789abcdef", k=16)),
55
+ "messages": messages,
56
+ "selectedModel": json_data.get("model", "grok-2-1212"),
57
+ }
58
+
59
+ chunk_id = "chipling-xai-" + "".join(random.choices("0123456789abcdef", k=32))
60
+ created = int(asyncio.get_event_loop().time())
61
+ total_tokens = 0
62
+
63
+ try:
64
+ async with httpx.AsyncClient(timeout=None) as client:
65
+ async with client.stream(
66
+ "POST",
67
+ "https://ai-sdk-starter-xai.vercel.app/api/chat",
68
+ headers=XaiAPI.headers,
69
+ json=request_data
70
+ ) as request_ctx:
71
+ if request_ctx.status_code == 200:
72
+ async for line in request_ctx.aiter_lines():
73
+ if line:
74
+ if line.startswith('0:'):
75
+ # Clean up the text and properly escape JSON characters
76
+ text = line[2:].strip()
77
+ if text.startswith('"') and text.endswith('"'):
78
+ text = text[1:-1]
79
+ text = text.replace('\\n', '\n').replace('\\', '')
80
+
81
+ response = {
82
+ "id": chunk_id,
83
+ "object": "chat.completion.chunk",
84
+ "created": created,
85
+ "model": json_data.get("model", "grok-2-1212"),
86
+ "choices": [{
87
+ "index": 0,
88
+ "text": text,
89
+ "logprobs": None,
90
+ "finish_reason": None
91
+ }],
92
+ "usage": None
93
+ }
94
+ yield f"data: {json.dumps(response)}\n\n"
95
+ total_tokens += 1
96
+ elif line.startswith('d:'):
97
+ final = {
98
+ "id": chunk_id,
99
+ "object": "chat.completion.chunk",
100
+ "created": created,
101
+ "model": json_data.get("model", "grok-2-1212"),
102
+ "choices": [],
103
+ "usage": {
104
+ "prompt_tokens": len(messages),
105
+ "completion_tokens": total_tokens,
106
+ "total_tokens": len(messages) + total_tokens
107
+ }
108
+ }
109
+ yield f"data: {json.dumps(final)}\n\n"
110
+ yield "data: [DONE]\n\n"
111
+ return
112
+ else:
113
+ yield f"data: [Unexpected status code: {request_ctx.status_code}]\n\n"
114
+ except Exception as e:
115
+ yield f"data: [Connection error: {str(e)}]\n\n"
116
+
117
+
118
+ class GroqAPI:
119
+
120
+ headers = {
121
+ 'accept': '*/*',
122
+ 'accept-language': 'en-US,en;q=0.9,ja;q=0.8',
123
+ 'content-type': 'application/json',
124
+ 'origin': 'https://ai-sdk-starter-groq.vercel.app',
125
+ 'priority': 'u=1, i',
126
+ 'referer': 'https://ai-sdk-starter-groq.vercel.app/',
127
+ 'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
128
+ 'sec-ch-ua-mobile': '?0',
129
+ 'sec-ch-ua-platform': '"macOS"',
130
+ 'sec-fetch-dest': 'empty',
131
+ 'sec-fetch-mode': 'cors',
132
+ 'sec-fetch-site': 'same-origin',
133
+ 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
134
+ }
135
+
136
+ def __init__(self):
137
+ self.base_url = "https://ai-sdk-starter-groq.vercel.app/api/chat"
138
+
139
+ def get_model_list(self):
140
+ models = ['meta-llama/llama-4-scout-17b-16e-instruct', 'llama-3.1-8b-instant', 'llama-3.3-70b-versatile', 'deepseek-r1-distill-llama-70b']
141
+ return models
142
+
143
+
144
+ async def generate(self, json_data: dict):
145
+ messages = XaiAPI.convert(json_data["messages"])
146
+
147
+ request_data = {
148
+ "id": "".join(random.choices("0123456789abcdef", k=16)),
149
+ "messages": messages,
150
+ "selectedModel": json_data.get("model", "deepseek-r1-distill-llama-70b"),
151
+ }
152
+
153
+ chunk_id = "chipling-groq-" + "".join(random.choices("0123456789abcdef", k=32))
154
+ created = int(asyncio.get_event_loop().time())
155
+ total_tokens = 0
156
+
157
+ try:
158
+ async with httpx.AsyncClient(timeout=None) as client:
159
+ async with client.stream(
160
+ "POST",
161
+ "https://ai-sdk-starter-groq.vercel.app/api/chat",
162
+ headers=GroqAPI.headers,
163
+ json=request_data
164
+ ) as request_ctx:
165
+ print(request_ctx.status_code)
166
+ if request_ctx.status_code == 200:
167
+ async for line in request_ctx.aiter_lines():
168
+ if line:
169
+ if line.startswith('0:'):
170
+ # Clean up the text and properly escape JSON characters
171
+ text = line[2:].strip()
172
+ if text.startswith('"') and text.endswith('"'):
173
+ text = text[1:-1]
174
+ text = text.replace('\\n', '\n').replace('\\', '')
175
+
176
+ response = {
177
+ "id": chunk_id,
178
+ "object": "chat.completion.chunk",
179
+ "created": created,
180
+ "model": json_data.get("model", "deepseek-r1-distill-llama-70b"),
181
+ "choices": [{
182
+ "index": 0,
183
+ "text": text,
184
+ "logprobs": None,
185
+ "finish_reason": None
186
+ }],
187
+ "usage": None
188
+ }
189
+ yield f"data: {json.dumps(response)}\n\n"
190
+ total_tokens += 1
191
+ elif line.startswith('d:'):
192
+ final = {
193
+ "id": chunk_id,
194
+ "object": "chat.completion.chunk",
195
+ "created": created,
196
+ "model": json_data.get("model", "deepseek-r1-distill-llama-70b"),
197
+ "choices": [],
198
+ "usage": {
199
+ "prompt_tokens": len(messages),
200
+ "completion_tokens": total_tokens,
201
+ "total_tokens": len(messages) + total_tokens
202
+ }
203
+ }
204
+ yield f"data: {json.dumps(final)}\n\n"
205
+ yield "data: [DONE]\n\n"
206
+ return
207
+ else:
208
+ yield f"data: [Unexpected status code: {request_ctx.status_code}]\n\n"
209
+ except Exception as e:
210
+ yield f"data: [Connection error: {str(e)}]\n\n"
211
+
212
+
213
+ class DeepinfraAPI:
214
+ headers = {
215
+ 'accept': '*/*',
216
+ 'accept-language': 'en-US,en;q=0.9,ja;q=0.8',
217
+ 'content-type': 'application/json',
218
+ 'origin': 'https://ai-sdk-starter-deepinfra.vercel.app',
219
+ 'priority': 'u=1, i',
220
+ 'referer': 'https://ai-sdk-starter-deepinfra.vercel.app/',
221
+ 'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
222
+ 'sec-ch-ua-mobile': '?0',
223
+ 'sec-ch-ua-platform': '"macOS"',
224
+ 'sec-fetch-dest': 'empty',
225
+ 'sec-fetch-mode': 'cors',
226
+ 'sec-fetch-site': 'same-origin',
227
+ 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36'
228
+ }
229
+
230
+ def __init__(self):
231
+ self.base_url = "https://ai-sdk-starter-deepinfra.vercel.app/api/chat"
232
+
233
+ def get_model_list(self):
234
+ models = ["deepseek-ai/DeepSeek-R1", "meta-llama/Llama-3.3-70B-Instruct-Turbo", "Qwen/Qwen2.5-72B-Instruct"]
235
+ return models
236
+
237
+ async def generate(self, json_data: dict):
238
+ messages = XaiAPI.convert(json_data["messages"])
239
+
240
+ request_data = {
241
+ "id": "".join(random.choices("0123456789abcdef", k=16)),
242
+ "messages": messages,
243
+ "selectedModel": json_data.get("model"),
244
+ }
245
+
246
+ chunk_id = "chipling-deepinfra-" + "".join(random.choices("0123456789abcdef", k=32))
247
+ created = int(asyncio.get_event_loop().time())
248
+ total_tokens = 0
249
+
250
+ try:
251
+ async with httpx.AsyncClient(timeout=None) as client:
252
+ async with client.stream(
253
+ "POST",
254
+ self.base_url,
255
+ headers=DeepinfraAPI.headers,
256
+ json=request_data
257
+ ) as request_ctx:
258
+ if request_ctx.status_code == 200:
259
+ async for line in request_ctx.aiter_lines():
260
+ if line:
261
+ if line.startswith('0:'):
262
+ text = line[2:].strip()
263
+ if text.startswith('"') and text.endswith('"'):
264
+ text = text[1:-1]
265
+ text = text.replace('\\n', '\n').replace('\\', '')
266
+
267
+ response = {
268
+ "id": chunk_id,
269
+ "object": "chat.completion.chunk",
270
+ "created": created,
271
+ "model": json_data.get("model", "deepseek-ai/DeepSeek-R1"),
272
+ "choices": [{
273
+ "index": 0,
274
+ "text": text,
275
+ "logprobs": None,
276
+ "finish_reason": None
277
+ }],
278
+ "usage": None
279
+ }
280
+ yield f"data: {json.dumps(response)}\n\n"
281
+ total_tokens += 1
282
+ elif line.startswith('d:'):
283
+ final = {
284
+ "id": chunk_id,
285
+ "object": "chat.completion.chunk",
286
+ "created": created,
287
+ "model": json_data.get("model", "deepseek-ai/DeepSeek-R1"),
288
+ "choices": [],
289
+ "usage": {
290
+ "prompt_tokens": len(messages),
291
+ "completion_tokens": total_tokens,
292
+ "total_tokens": len(messages) + total_tokens
293
+ }
294
+ }
295
+ yield f"data: {json.dumps(final)}\n\n"
296
+ yield "data: [DONE]\n\n"
297
+ return
298
+ else:
299
+ yield f"data: [Unexpected status code: {request_ctx.status_code}]\n\n"
300
+ except Exception as e:
301
+ yield f"data: [Connection error: {str(e)}]\n\n"
test.py CHANGED
@@ -1,35 +1,52 @@
1
- import requests
2
- import json
3
 
4
- messages = [
5
- {"role": "user", "content": "helo"},
6
- {"role": "assistant", "content": "Hello! How can I assist you today?"},
7
- {"role": "user", "content": "who are you and give me a breif description of who created you "}
8
- ]
9
 
10
- model = "grok-3-mini"
11
 
12
- url = " http://127.0.0.1:8000/api/v1/generate"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- payload = {
15
- "messages": messages,
16
- "model": model
17
- }
18
 
19
- response = requests.post(url, json=payload, stream=True)
 
 
 
 
 
 
 
20
 
 
21
  if response.status_code == 200:
22
- for line in response.iter_lines():
23
- if line:
24
- decoded_line = line.decode('utf-8')
25
- if decoded_line.startswith('data: [DONE]'):
26
- break
27
- elif decoded_line.startswith('data: '):
28
- try:
29
- json_data = json.loads(decoded_line[6:])
30
- if json_data["choices"] and "text" in json_data["choices"][0]:
31
- print(json_data["choices"][0]["text"], end='')
32
- except json.JSONDecodeError:
33
- continue
34
- else:
35
- print(f"Request failed with status code {response.status_code}")
 
1
+ # import requests
2
+ # import json
3
 
4
+ # messages = [
5
+ # {"role": "user", "content": "helo"},
6
+ # {"role": "assistant", "content": "Hello! How can I assist you today?"},
7
+ # {"role": "user", "content": "who are you and give me a breif description of who created you "}
8
+ # ]
9
 
10
+ # model = "Qwen/Qwen2.5-72B-Instruct"
11
 
12
+ # url = " http://127.0.0.1:8000/api/v1/generate"
13
+
14
+ # payload = {
15
+ # "messages": messages,
16
+ # "model": model
17
+ # }
18
+
19
+ # response = requests.post(url, json=payload, stream=True)
20
+
21
+ # if response.status_code == 200:
22
+ # for line in response.iter_lines():
23
+ # if line:
24
+ # decoded_line = line.decode('utf-8')
25
+ # if decoded_line.startswith('data: [DONE]'):
26
+ # break
27
+ # elif decoded_line.startswith('data: '):
28
+ # try:
29
+ # json_data = json.loads(decoded_line[6:])
30
+ # if json_data["choices"] and "text" in json_data["choices"][0]:
31
+ # print(json_data["choices"][0]["text"], end='')
32
+ # except json.JSONDecodeError:
33
+ # continue
34
+ # else:
35
+ # print(f"Request failed with status code {response.status_code}")
36
 
 
 
 
 
37
 
38
+ import requests
39
+
40
+ url = 'http://127.0.1:8000/api/v1/generate-images'
41
+
42
+ query = {
43
+ 'prompt': 'a beautiful landscape',
44
+ 'model': 'black-forest-labs/FLUX.1-dev',
45
+ }
46
 
47
+ response = requests.post(url, json=query)
48
  if response.status_code == 200:
49
+ data = response.json()
50
+ print(data)
51
+ else:
52
+ print(f"Error: {response.status_code} - {response.text}")