-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update code structure for huggingface deployment
- Loading branch information
Mark Chen
authored and
Mark Chen
committed
Feb 9, 2024
1 parent
5f20563
commit d05438e
Showing
17 changed files
with
1,945 additions
and
136 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
File renamed without changes.
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.