To deploy a LangChain application using GPT-4o on Hugging Face’s serverless inference API, you can use Hugging Face Spaces with Gradio or FastAPI. Below is a step-by-step guide.
Hugging Face Spaces support FastAPI, Gradio, and Flask. We will use FastAPI for a serverless deployment.
- Go to Hugging Face Spaces.
- Click “Create new Space”.
- Select Docker as the environment (best for flexibility).
- Clone the repository to your local system.
Create a requirements.txt file with:
Alternatively, install them locally:
pip install fastapi uvicorn openai langchain langchain-openai
Inside your Space, create an app.py file:
from fastapi import FastAPI
from pydantic import BaseModel
from langchain_openai import ChatOpenAI
# Set API key from Hugging Face Secrets (Recommended)
os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
# Initialize GPT-4o model
llm = ChatOpenAI(model="gpt-4o")
class PromptInput(BaseModel):
def generate_response(data: PromptInput):
response = llm.invoke(data.prompt)
return {"response": response}
Create a Dockerfile to containerize the app for Hugging Face Spaces:
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
-
Push your changes to the Hugging Face Space repository.
-
Hugging Face will automatically build and deploy your app.
-
Your API will be available at:
https://your-space-name.hf.space/generate
Once deployed, test it using cURL or Python:
curl -X POST "https://your-space-name.hf.space/generate" \
-H "Content-Type: application/json" \
-d '{"prompt": "Explain serverless computing."}'
Or in Python:
url = "https://your-space-name.hf.space/generate"
data = {"prompt": "Explain serverless computing."}
response = requests.post(url, json=data)
If you want a simple web UI instead of an API, use Gradio:
return llm.invoke(prompt)
iface = gr.Interface(fn=chat, inputs="text", outputs="text")
Now, you have successfully deployed a LangChain + GPT-4o application on Hugging Face’s serverless infrastructure! 🚀
Would you like to customize it further? Let me know!