File size: 1,622 Bytes
9e55bb1
 
 
 
 
 
1173390
9e55bb1
 
 
 
 
e3ed510
 
9e55bb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335c7a7
1173390
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbc88fd
 
 
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline

app = FastAPI()

# Define request model for math operations
class CalculationRequest(BaseModel):
    a: float
    b: float
    operation: str

# Load a smaller Hugging Face model (example: distilgpt2)
model = pipeline('text-generation', model='distilgpt2')

@app.post("/calculate")
def calculate(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}

# Example endpoint using Hugging Face model
@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']}

# New endpoint for testing math operations
@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=8000)