test-api / app.py
chetanganatra's picture
Update app.py
528fb02 verified
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)
'''