Noor22Tak's picture
Update app.py
9f61ac9 verified
raw
history blame contribute delete
937 Bytes
from fastapi import FastAPI, Form
import requests
import os
from requests.exceptions import RequestException
app = FastAPI()
API_URL = "https://api-inference.huggingface.co/models/Noor22Tak/autotrain-6k0fa-2222"
HF_TOKEN = os.getenv("HF_TOKEN")
headers = {
"Authorization": f"Bearer {HF_TOKEN}"
}
@app.post("/predict")
def predict(description: str = Form(...)):
try:
payload = {"inputs": description}
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status()
result = response.json()
return {"result": result}
except RequestException as e:
return {"error": f"Request failed: {e}"}
except ValueError:
return {"error": "Failed to decode the response JSON"}
except Exception as e:
return {"error": f"An unexpected error occurred: {e}"}
@app.get('/')
def welcome():
return {"message" : 'The Space is Running'}