SeaLLM-Chat / app.py
NGUYEN, Xuan Phi
update
d3c19b3
raw
history blame
3.67 kB
# Copyright: DAMO Academy, Alibaba Group
# By Xuan Phi Nguyen at DAMO Academy, Alibaba Group
# Description:
"""
Demo script to launch Language chat model
"""
try:
import spaces
except ModuleNotFoundError:
print(f'Cannot import hf `spaces` with `import spaces`.')
import os
from gradio.themes import ThemeClass as Theme
import numpy as np
import argparse
# import torch
import gradio as gr
from typing import Any, Iterator
from typing import Iterator, List, Optional, Tuple
import filelock
import glob
import json
import time
from gradio.routes import Request
from gradio.utils import SyncToAsyncIterator, async_iteration
from gradio.helpers import special_args
import anyio
from typing import AsyncGenerator, Callable, Literal, Union, cast
from gradio_client.documentation import document, set_documentation_group
from typing import List, Optional, Union, Dict, Tuple
from tqdm.auto import tqdm
from huggingface_hub import snapshot_download
from langchain_community.embeddings import HuggingFaceEmbeddings, HuggingFaceBgeEmbeddings
from gradio.components import Button, Component
from gradio.events import Dependency, EventListenerMethod
from multipurpose_chatbot.demos.base_demo import CustomTabbedInterface
from multipurpose_chatbot.configs import (
MODEL_TITLE,
MODEL_DESC,
MODEL_INFO,
CITE_MARKDOWN,
ALLOWED_PATHS,
PROXY,
PORT,
MODEL_PATH,
MODEL_NAME,
BACKEND,
DEMOS,
DELETE_FOLDER,
)
demo = None
if DELETE_FOLDER is not None and os.path.exists(DELETE_FOLDER):
print(F'WARNING deleting folder: {DELETE_FOLDER}')
import shutil
print(f'DELETE ALL FILES IN {DELETE_FOLDER}')
for filename in os.listdir(DELETE_FOLDER):
file_path = os.path.join(DELETE_FOLDER, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
print(f'deleted: {file_path}')
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
def launch_demo():
global demo, MODEL_ENGINE
model_desc = MODEL_DESC
model_path = MODEL_PATH
print(f'Begin importing models')
from multipurpose_chatbot.demos import get_demo_class
# demos = {
# k: get_demo_class(k)().create_demo()
# for k in demo_and_tab_names.keys()
# }
print(f'{DEMOS=}')
demo_class_objects = {
k: get_demo_class(k)()
for k in DEMOS
}
demos = {
k: get_demo_class(k)().create_demo()
for k in DEMOS
}
demos_names = [x.tab_name for x in demo_class_objects.values()]
descriptions = model_desc
if MODEL_INFO is not None and MODEL_INFO != "":
descriptions += (
f"<br>" +
MODEL_INFO.format(model_path=model_path)
)
if len(demos) == 1:
demo = demos[DEMOS[0]]
else:
demo = CustomTabbedInterface(
interface_list=list(demos.values()),
tab_names=demos_names,
title=f"{MODEL_TITLE}",
description=descriptions,
)
demo.title = MODEL_NAME
with demo:
gr.Markdown(CITE_MARKDOWN)
demo.queue(api_open=False)
return demo
if __name__ == "__main__":
demo = launch_demo()
if PROXY is not None and PROXY != "":
print(f'{PROXY=} {PORT=}')
print(f"{ALLOWED_PATHS=}")
demo.launch(server_port=PORT, root_path=PROXY, show_api=False, allowed_paths=ALLOWED_PATHS)
else:
demo.launch(server_port=PORT, show_api=False, allowed_paths=ALLOWED_PATHS)