Skip to content

Commit

Permalink
update code structure for huggingface deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Chen authored and Mark Chen committed Feb 9, 2024
1 parent 5f20563 commit d05438e
Show file tree
Hide file tree
Showing 17 changed files with 1,945 additions and 136 deletions.
Binary file added .DS_Store
Binary file not shown.
7 changes: 0 additions & 7 deletions .vscode/settings.json

This file was deleted.

35 changes: 32 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
# Use the custom FastAPI image
# FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
# Use ubuntu as base image
FROM ubuntu:20.04

# Avoid prompts from apt
ENV DEBIAN_FRONTEND=noninteractive

# Update apt repositories and install Python and Pip
RUN apt-get update && \
apt-get install -y python3-pip python3-dev

# Check if the symbolic links already exist and create them if they don't
RUN if [ ! -e /usr/bin/python ]; then ln -s /usr/bin/python3 /usr/bin/python; fi && \
if [ ! -e /usr/bin/pip ]; then ln -s /usr/bin/pip3 /usr/bin/pip; fi

# Set default values for environment variables
ENV OPENAI_ORG_ID=default_org_id
ENV OPENAI_API_KEY=default_api_key

COPY ./requirements.txt /code/requirements.txt
# Set environment variables for Matplotlib and Fontconfig
ENV MPLCONFIGDIR=/app/matplotlib_cache
ENV FONTCONFIG_PATH=/app/fontconfig

# Create the directories for Matplotlib cache and Fontconfig
RUN mkdir -p /app/matplotlib_cache /app/fontconfig && \
chmod -R 777 /app/matplotlib_cache /app/fontconfig

# Create a writable directory for Fontconfig cache
RUN mkdir -p /app/fontconfig_cache && chmod -R 777 /app/fontconfig_cache

# Set the environment variable so Fontconfig uses the writable directory
ENV FONTCONFIG_PATH=/app/fontconfig_cache

# Copy the requirements file and install dependencies
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# Copy your application source code and script
COPY ./src /app

CMD ["uvicorn", "index.main:app", "--host", "0.0.0.0", "--port", "80"]
CMD ["uvicorn", "app.app.main:app", "--host", "0.0.0.0", "--port", "7860"]
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
---
title: Building your generative AI Apps in Python
emoji: 🤖
colorFrom: "green"
colorTo: "green"
sdk: docker
sdk_version: 3.8.5
app_file: app.py
pinned: false
---
# 生成式AI後端開發部署實戰
# Building your generative AI API in Python

Expand Down
Binary file added scripts/.DS_Store
Binary file not shown.
44 changes: 44 additions & 0 deletions scripts/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import gradio as gr
import requests

is_gpt3 = True


def echo_gptchatbot(message, history):
# Post the message to the server
response = requests.post(
"https://markchenx-udemy-genai-app-course.hf.space/api/chat/gpt3", data={"user_request": message}
)
# Return the response
llm_output = response.json()["result"]["content"]

return llm_output


def echo_llamachatbot(message, history):
# Post the message to the server
response = requests.post(
"https://markchenx-udemy-genai-app-course.hf.space/api/chat/llama", data={"user_request": message}
)

# Return the response
llm_output = response.json()["result"][0]["generated_text"]

return llm_output


# Create a Gradio interface with the chatbot
if is_gpt3:
demo = gr.ChatInterface(
fn=echo_gptchatbot,
examples=["What is OpenAI?", "What is GPT-3?"],
title="GPT-3 Chatbot",
)
else:
demo = gr.ChatInterface(
fn=echo_llamachatbot,
examples=["What is OpenAI?", "What is LLM?"],
title="LLM Chatbot - Llama 2 7B",
)

demo.launch()
6 changes: 3 additions & 3 deletions scripts/app_start.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Run two commands in the background at the same time
cd ../src
python3 -m uvicorn index.main:app --reload & python3 app/main.py
#!/bin/sh
# Run the python module while starting the python app with ./app/main.py
python3 -m uvicorn index.main:app --host 0.0.0.0 --port 8000 #& python3 src_app/gradio_main.py
Binary file added src/.DS_Store
Binary file not shown.
File renamed without changes.
Empty file added src/app/ai/__init__.py
Empty file.
File renamed without changes.
76 changes: 44 additions & 32 deletions src/app/main.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,56 @@
import gradio as gr
import requests
from fastapi import FastAPI, Form
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
from .ai.chatbot import gpt_chatbot, llama_chatbot

is_gpt3 = False
isProduction = False

origins = ["*"]

def echo_gptchatbot(message, history):
# Post the message to the server
response = requests.post(
"http://127.0.0.1:8000/api/chat/gpt3", data={"user_request": message}
if isProduction:
app = FastAPI(
title="LLM API Endpoints",
docs_url=None, # Disable docs (Swagger UI)
redoc_url=None, # Disable redoc
)
# Return the response
llm_output = response.json()["result"]["content"]
#app.mount("/static", StaticFiles(directory="static"), name="static")
else:
app = FastAPI(title="LLM API Endpoints")
#app.mount("/static", StaticFiles(directory="static"), name="static")

return llm_output
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["POST", "GET", "PUT", "DELETE"],
allow_headers=["*"],
)


def echo_llamachatbot(message, history):
# Post the message to the server
response = requests.post(
"http://127.0.0.1:8000/api/chat/llama", data={"user_request": message}
)
# Create a homepage route
@app.get("/")
async def index():
return {"server ok": True}

# Return the response
llm_output = response.json()["result"][0]["generated_text"]

return llm_output
@app.post("/api/chat/gpt3", tags=["OpenAI GPT-3"])
async def gpt_chat(user_request: str = Form(...)):
"""
Chat with LLM Backend - GPT-3
"""
# Get the text content in the user request
result = gpt_chatbot(user_request=user_request)

return {"result": result}

# Create a Gradio interface with the chatbot
if is_gpt3:
demo = gr.ChatInterface(
fn=echo_gptchatbot,
examples=["What is OpenAI?", "What is GPT-3?"],
title="GPT-3 Chatbot",
)
else:
demo = gr.ChatInterface(
fn=echo_llamachatbot,
examples=["What is OpenAI?", "What is LLM?"],
title="LLM Chatbot - Llama 2 7B",
)

demo.launch()
@app.post("/api/chat/llama", tags=["Llama 2 7B Chat"])
async def llama_chat(user_request: str = Form(...)):
"""
Chat with LLM Backend - Llama 2 7b Chat
"""
# Get the text content in the user request
result = llama_chatbot(user_request=user_request)

return {"result": result}
76 changes: 0 additions & 76 deletions src/index/main.py

This file was deleted.

Binary file removed src/static/favicon.ico
Binary file not shown.
2 changes: 1 addition & 1 deletion src/test_main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fastapi.testclient import TestClient
from index.main import app
from app.main import app

client = TestClient(app)

Expand Down
14 changes: 0 additions & 14 deletions vercel.json

This file was deleted.

Loading

0 comments on commit d05438e

Please sign in to comment.