File size: 5,426 Bytes
7a4b76e
14cd2f6
9edc2f1
7a4b76e
 
08606b0
5bf1b3e
54ac845
7a4b76e
14cd2f6
7a4b76e
14cd2f6
7a4b76e
14cd2f6
 
 
 
 
 
7a4b76e
 
 
 
 
14cd2f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a4b76e
 
54ac845
 
 
7a4b76e
 
 
14cd2f6
9edc2f1
 
14cd2f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f74fc96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
745d3f1
7a4b76e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import time
from fastapi import FastAPI, Request, HTTPException
from pydantic import BaseModel
from duckai import DuckAI
import uvicorn

app = FastAPI()
proxy= "socks4://98.178.72.21:10919"
API_PREFIX = "/"

# Middleware for logging request time
@app.middleware("http")
async def log_process_time(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    print(f"{request.method} {response.status_code} {request.url.path} {process_time*1000:.2f} ms")
    return response

# Request body model
class ChatCompletionRequest(BaseModel):
    model: str
    messages: list[dict]

@app.get("/")
async def root():
    return {"message": "API server running"}

@app.get("/ping")
async def ping():
    return {"message": "pong"}

@app.get(f"{API_PREFIX}v1/models")
async def get_models():
    return {
        "object": "list",
        "data": [
            {"id": "gpt-4o-mini", "object": "model", "owned_by": "ddg"},
            {"id": "claude-3-haiku", "object": "model", "owned_by": "ddg"},
            {"id": "llama-3.1-70b", "object": "model", "owned_by": "ddg"},
            {"id": "mixtral-8x7b", "object": "model", "owned_by": "ddg"},
            {"id": "o3-mini", "object": "model", "owned_by": "ddg"},
        ],
    }

@app.post(f"{API_PREFIX}v1/chat/completions")
async def chat_completions(request: ChatCompletionRequest):
    try:
        # Only using DuckAI directly
        content = " ".join([msg.get("content", "") for msg in request.messages])


        duck = DuckAI(proxy=proxy, timeout=20)
        results = duck.chat(content, model=request.model)
        response = create_complete_response(results, request.model)
        return response

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

def create_complete_response(text: str, model: str) -> dict:
    """Create a complete non-streaming response"""
    return {
        "id": "chatcmpl-123",
        "object": "chat.completion",
        "created": int(time.time()),
        "model": model,
        "usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
        "choices": [
            {
                "message": {"content": text, "role": "assistant"},
                "index": 0,
                "finish_reason": "stop",
            },
        ],
    }

@app.get("/in/10th")
async def get_tenth_class_data():
    return {
        "class": "10",
        "subjects": [
            {
                "name": "English",
                "chapters": [
                    {"id": "eng-1", "name": "Prose: A Letter to God", "level": "easy"},
                    {"id": "eng-2", "name": "Poem: Dust of Snow", "level": "medium"},
                    {"id": "eng-3", "name": "Prose: Nelson Mandela", "level": "hard"},
                    {"id": "eng-4", "name": "Poem: Fire and Ice", "level": "medium"},
                    {"id": "eng-5", "name": "Prose: From the Diary of Anne Frank", "level": "easy"}
                ]
            },
            {
                "name": "Math",
                "chapters": [
                    {"id": "math-1", "name": "Real Numbers", "level": "medium"},
                    {"id": "math-2", "name": "Polynomials", "level": "medium"},
                    {"id": "math-3", "name": "Pair of Linear Equations in Two Variables", "level": "hard"},
                    {"id": "math-4", "name": "Quadratic Equations", "level": "hard"},
                    {"id": "math-5", "name": "Arithmetic Progressions", "level": "easy"}
                ]
            },
            {
                "name": "Science",
                "chapters": [
                    {"id": "sci-1", "name": "Chemical Reactions and Equations", "level": "easy"},
                    {"id": "sci-2", "name": "Acids, Bases and Salts", "level": "medium"},
                    {"id": "sci-3", "name": "Metals and Non-Metals", "level": "medium"},
                    {"id": "sci-4", "name": "Life Processes", "level": "hard"},
                    {"id": "sci-5", "name": "Electricity", "level": "medium"}
                ]
            },
            {
                "name": "History",
                "chapters": [
                    {"id": "hist-1", "name": "The Rise of Nationalism in Europe", "level": "hard"},
                    {"id": "hist-2", "name": "Nationalism in India", "level": "medium"},
                    {"id": "hist-3", "name": "The Making of a Global World", "level": "medium"},
                    {"id": "hist-4", "name": "The Age of Industrialization", "level": "hard"},
                    {"id": "hist-5", "name": "Print Culture and the Modern World", "level": "easy"}
                ]
            },
            {
                "name": "Hindi",
                "chapters": [
                    {"id": "hin-1", "name": "पाठ: पद", "level": "easy"},
                    {"id": "hin-2", "name": "पाठ: सूरदास", "level": "medium"},
                    {"id": "hin-3", "name": "पाठ: तुलसीदास", "level": "hard"},
                    {"id": "hin-4", "name": "पाठ: साखी", "level": "medium"},
                    {"id": "hin-5", "name": "पाठ: आत्मकथ्य", "level": "easy"}
                ]
            }
        ]
    }
if __name__ == "__main__":
    uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)