integration / app.py
shahed125238's picture
Update app.py
5203638 verified
Raw History Blame Contribute Delete
1.27 kB
from fastapi import FastAPI
from llama_index_llms_huggingface_api import HuggingFaceInferenceAPI
from pydantic import BaseModel
# Define your Pydantic model with protected namespaces disabled
class ModelConfig(BaseModel):
model_id: str
model_name: str
class Config:
protected_namespaces = () # Disable protected namespaces warning
# Define Settings class
class Settings:
llm = None # Initialize the llm attribute
# Initialize FastAPI app
app = FastAPI()
# Set up the HuggingFaceInferenceAPI with the new class
Settings.llm = HuggingFaceInferenceAPI(
api_key="your_api_key", # Replace with your actual API key
model_name="your_model_name" # Replace with your actual model name
)
# Sample endpoint to test the LLM
@app.post("/predict/")
async def predict(input_text: str):
try:
response = Settings.llm.generate(input_text) # Replace with the appropriate method for your API
return {"prediction": response}
except Exception as e:
return {"error": str(e)}
# Sample endpoint to retrieve model configuration
@app.get("/model-config/")
async def get_model_config():
return {"model_id": Settings.llm.model_id, "model_name": Settings.llm.model_name}
# Run the app using: uvicorn app:app --reload