Spaces:
Sleeping
Sleeping
File size: 1,679 Bytes
a7eb20d 72f5af3 345d10b 72f5af3 981d40d a7eb20d 72f5af3 e827c31 72f5af3 528fb02 |
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 |
from fastapi import FastAPI, Request
import uvicorn
app = FastAPI(title="Simple Uppercase API", description="Convert text to uppercase")
@app.get("/")
async def root():
return {"message": "Simple Uppercase API is running! Use POST /predict to convert text to uppercase."}
@app.post("/predict")
async def predict(request: Request):
try:
body = await request.json()
uid, question = body.get("data", [None, None])
# Check if we have valid input
if question is None:
return {
"error": "Invalid input format. Expected: {'data': ['uid', 'text_to_convert']}"
}
# Convert to uppercase
result = question.upper()
return {
"uid": uid,
"original_text": question,
"uppercase_text": result,
"status": "success"
}
except Exception as e:
return {
"error": f"Error processing request: {str(e)}",
"status": "error"
}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) # Port 7860 is standard for HF Spaces
'''
How to call this via python script:
import requests
# Replace with your actual space name
SPACE_NAME = "chetanganatra/test-api"
API_URL = f"https://{SPACE_NAME.replace('/', '-')}.hf.space/predict"
# Prepare the data
data = {
"data": ["user123", "What is machine learning?"]
}
# Make the API call
response = requests.post(API_URL, json=data)
if response.status_code == 200:
result = response.json()
print(result)
else:
print(f"Error: {response.status_code}")
print(response.text)
''' |