Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Define request model
|
| 8 |
+
class CalculationRequest(BaseModel):
|
| 9 |
+
a: float
|
| 10 |
+
b: float
|
| 11 |
+
operation: str
|
| 12 |
+
|
| 13 |
+
# Load Hugging Face model (example: a simple text generation model)
|
| 14 |
+
model = pipeline('text-generation', model='gpt2')
|
| 15 |
+
|
| 16 |
+
@app.post("/calculate")
|
| 17 |
+
def calculate(request: CalculationRequest):
|
| 18 |
+
a = request.a
|
| 19 |
+
b = request.b
|
| 20 |
+
operation = request.operation
|
| 21 |
+
|
| 22 |
+
if operation == "add":
|
| 23 |
+
result = a + b
|
| 24 |
+
elif operation == "subtract":
|
| 25 |
+
result = a - b
|
| 26 |
+
elif operation == "multiply":
|
| 27 |
+
result = a * b
|
| 28 |
+
elif operation == "divide":
|
| 29 |
+
result = a / b
|
| 30 |
+
else:
|
| 31 |
+
return {"error": "Invalid operation"}
|
| 32 |
+
|
| 33 |
+
return {"result": result}
|
| 34 |
+
|
| 35 |
+
# Example endpoint using Hugging Face model
|
| 36 |
+
@app.post("/generate")
|
| 37 |
+
def generate_text(prompt: str):
|
| 38 |
+
generated = model(prompt, max_length=50)
|
| 39 |
+
return {"generated_text": generated[0]['generated_text']}
|