File size: 3,285 Bytes
9e55bb1
 
 
 
 
 
 
 
 
 
 
e3ed510
9e55bb1
4493d75
 
 
 
9e55bb1
 
 
 
406da8a
9e55bb1
406da8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e55bb1
 
 
 
 
335c7a7
1173390
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbc88fd
 
 
 
3c2a6d1
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
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline

app = FastAPI()

class CalculationRequest(BaseModel):
    a: float
    b: float
    operation: str

model = pipeline('text-generation', model='distilgpt2')

@app.get("/")
def read_root():
    return {"message": "Welcome to the Math API"}

@app.post("/calculate")
def calculate(request: CalculationRequest):
    a = request.a
    b = request.b
    operation = request.operation.lower()

    try:
        if operation == "add":
            result = a + b
        elif operation == "subtract":
            result = a - b
        elif operation == "multiply":
            result = a * b
        elif operation == "divide":
            if b == 0:
                raise HTTPException(status_code=400, detail="Division by zero is not allowed")
            result = a / b
        elif operation == "power":
            result = math.pow(a, b)
        elif operation == "modulus":
            result = a % b
        elif operation == "sqrt":
            if a < 0:
                raise HTTPException(status_code=400, detail="Square root of negative number is not allowed")
            result = math.sqrt(a)
        elif operation == "log":
            if a <= 0:
                raise HTTPException(status_code=400, detail="Logarithm of non-positive number is not allowed")
            result = math.log(a)
        elif operation == "log10":
            if a <= 0:
                raise HTTPException(status_code=400, detail="Logarithm of non-positive number is not allowed")
            result = math.log10(a)
        elif operation == "sin":
            result = math.sin(math.radians(a))
        elif operation == "cos":
            result = math.cos(math.radians(a))
        elif operation == "tan":
            result = math.tan(math.radians(a))
        elif operation == "asin":
            result = math.degrees(math.asin(a))
        elif operation == "acos":
            result = math.degrees(math.acos(a))
        elif operation == "atan":
            result = math.degrees(math.atan(a))
        elif operation == "factorial":
            if a < 0 or not float(a).is_integer():
                raise HTTPException(status_code=400, detail="Factorial of negative or non-integer number is not allowed")
            result = math.factorial(int(a))
        else:
            raise HTTPException(status_code=400, detail="Invalid operation")
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

    return {"result": result}

@app.post("/generate")
def generate_text(prompt: str):
    generated = model(prompt, max_length=50, clean_up_tokenization_spaces=True)
    return {"generated_text": generated[0]['generated_text']}

@app.post("/test_math")
def test_math(request: CalculationRequest):
    a = request.a
    b = request.b
    operation = request.operation

    if operation == "add":
        result = a + b
    elif operation == "subtract":
        result = a - b
    elif operation == "multiply":
        result = a * b
    elif operation == "divide":
        result = a / b
    else:
        return {"error": "Invalid operation"}

    return {"result": result}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)