qa1145 commited on
Commit
bf0b08e
·
verified ·
1 Parent(s): 6784fde

Upload 9 files

Browse files
Files changed (1) hide show
  1. app.py +16 -43
app.py CHANGED
@@ -1,14 +1,9 @@
1
  import gradio as gr
2
  from fastapi import FastAPI, Request, HTTPException
3
- from fastapi.responses import JSONResponse, StreamingResponse
4
- from pydantic import BaseModel
5
- from typing import List, Optional, Union
6
- import asyncio
7
  import random
8
- from datetime import datetime
9
  import threading
10
  import uvicorn
11
- import json
12
  import aiohttp
13
 
14
  from src.config import get_api_keys
@@ -46,26 +41,22 @@ async def list_models():
46
  return {"object": "list", "data": models}
47
 
48
 
49
- async def proxy_request(model_hint: Optional[str], messages: list, stream: bool):
50
- """透传请求到OpenRouter,只修改model"""
51
- api_keys = get_api_keys()
52
- api_key = random.choice(api_keys)
53
 
54
- # 找到可用的模型ID
55
  model_tester.refresh_model_list()
56
  available_free = model_tester.get_all_free_models()
57
 
 
58
  target_model = None
59
 
60
- if model_hint:
61
- # 尝试匹配用户指定的模型
62
  for m in available_free:
63
  model_name = m.replace(":free", "").split("/")[-1]
64
  if model_hint.lower() in model_name.lower():
65
  target_model = m
66
  break
67
- if not target_model:
68
- target_model = f"{model_hint}:free" if ":free" not in model_hint else model_hint
69
 
70
  if not target_model and available_free:
71
  target_model = random.choice(available_free[:5])
@@ -73,42 +64,35 @@ async def proxy_request(model_hint: Optional[str], messages: list, stream: bool)
73
  if not target_model:
74
  raise HTTPException(status_code=400, detail="No available model")
75
 
76
- # 构建请求
 
77
  url = "https://openrouter.ai/api/v1/chat/completions"
78
  headers = {
79
  "Authorization": f"Bearer {api_key}",
80
  "Content-Type": "application/json"
81
  }
82
- payload = {
83
- "model": target_model,
84
- "messages": messages,
85
- "stream": stream
86
- }
87
 
88
  async with aiohttp.ClientSession() as session:
89
- async with session.post(url, json=payload, headers=headers, timeout=aiohttp.ClientTimeout(total=120)) as response:
90
- if stream:
91
  async for chunk in response.content:
92
  yield chunk
93
  else:
94
- data = await response.json()
95
- yield data
96
 
97
 
98
  @fastapi_app.post("/v1/chat/completions")
99
  async def chat_completions(request: Request):
100
  body = await request.json()
101
- stream = body.get("stream", False)
102
- messages = body.get("messages", [])
103
 
104
- if stream:
105
  return StreamingResponse(
106
- proxy_request(body.get("model"), messages, stream),
107
  media_type="text/event-stream"
108
  )
109
 
110
  result = None
111
- async for data in proxy_request(body.get("model"), messages, stream):
112
  result = data
113
  break
114
 
@@ -126,14 +110,6 @@ async def health():
126
  return {"status": "ok"}
127
 
128
 
129
- def get_available_models():
130
- return model_tester.get_available_models(free_only=False)
131
-
132
-
133
- def get_available_free_models():
134
- return model_tester.get_available_models(free_only=True)
135
-
136
-
137
  def get_scan_status():
138
  scan_result = model_tester.scan_result
139
  total = scan_result.get("total_available", 0)
@@ -148,12 +124,9 @@ def format_model_list(models):
148
  with gr.Blocks(title="OpenRouter Free API") as demo:
149
  gr.Markdown("# OpenRouter Free API")
150
  gr.Markdown("Standard OpenAI-compatible API with free model support")
151
-
152
- gr.Markdown("## Status")
153
- gr.Markdown(f"**{get_scan_status()}**")
154
-
155
  gr.Markdown("## Available Free Models")
156
- gr.Textbox(value=format_model_list(get_available_free_models()), lines=15, interactive=False)
157
 
158
 
159
  app = gr.mount_gradio_app(fastapi_app, demo, path="/")
 
1
  import gradio as gr
2
  from fastapi import FastAPI, Request, HTTPException
3
+ from fastapi.responses import StreamingResponse
 
 
 
4
  import random
 
5
  import threading
6
  import uvicorn
 
7
  import aiohttp
8
 
9
  from src.config import get_api_keys
 
41
  return {"object": "list", "data": models}
42
 
43
 
44
+ async def proxy_request(body: dict):
45
+ """透传请求,只替换 model 和 key"""
46
+ api_key = random.choice(get_api_keys())
 
47
 
 
48
  model_tester.refresh_model_list()
49
  available_free = model_tester.get_all_free_models()
50
 
51
+ model_hint = body.get("model")
52
  target_model = None
53
 
54
+ if model_hint and available_free:
 
55
  for m in available_free:
56
  model_name = m.replace(":free", "").split("/")[-1]
57
  if model_hint.lower() in model_name.lower():
58
  target_model = m
59
  break
 
 
60
 
61
  if not target_model and available_free:
62
  target_model = random.choice(available_free[:5])
 
64
  if not target_model:
65
  raise HTTPException(status_code=400, detail="No available model")
66
 
67
+ body["model"] = target_model
68
+
69
  url = "https://openrouter.ai/api/v1/chat/completions"
70
  headers = {
71
  "Authorization": f"Bearer {api_key}",
72
  "Content-Type": "application/json"
73
  }
 
 
 
 
 
74
 
75
  async with aiohttp.ClientSession() as session:
76
+ async with session.post(url, json=body, headers=headers, timeout=aiohttp.ClientTimeout(total=120)) as response:
77
+ if body.get("stream"):
78
  async for chunk in response.content:
79
  yield chunk
80
  else:
81
+ yield await response.json()
 
82
 
83
 
84
  @fastapi_app.post("/v1/chat/completions")
85
  async def chat_completions(request: Request):
86
  body = await request.json()
 
 
87
 
88
+ if body.get("stream"):
89
  return StreamingResponse(
90
+ proxy_request(body),
91
  media_type="text/event-stream"
92
  )
93
 
94
  result = None
95
+ async for data in proxy_request(body):
96
  result = data
97
  break
98
 
 
110
  return {"status": "ok"}
111
 
112
 
 
 
 
 
 
 
 
 
113
  def get_scan_status():
114
  scan_result = model_tester.scan_result
115
  total = scan_result.get("total_available", 0)
 
124
  with gr.Blocks(title="OpenRouter Free API") as demo:
125
  gr.Markdown("# OpenRouter Free API")
126
  gr.Markdown("Standard OpenAI-compatible API with free model support")
127
+ gr.Markdown(f"**Status: {get_scan_status()}**")
 
 
 
128
  gr.Markdown("## Available Free Models")
129
+ gr.Textbox(value=format_model_list(model_tester.get_available_models(free_only=True)), lines=15, interactive=False)
130
 
131
 
132
  app = gr.mount_gradio_app(fastapi_app, demo, path="/")