import os import pip import gradio as gr from epub2txt import epub2txt if not os.path.exists("./chatglm2-6b-int4.flm"): os.system("git clone --recurse-submodules https://github.com/ztxz16/fastllm.git") os.system("cd fastllm; mkdir build; cd build; cmake ..; make -j") # os.system("cd fastllm; mkdir build; cd build; cmake ..; make -j; cd tools; python setup.py install --user --prefix=") # os.system("wget https://huggingface.co/huangyuyang/chatglm2-6b-int4.flm/resolve/main/chatglm2-6b-int4.flm") pip.main(['install', '-e', 'fastllm/build/tools/', '--user']) # exit() from fastllm_pytools import llm model = llm.model("./chatglm2-6b-int4.flm") prompt = os.getenv("prompt") class GUI: def __init__(self, *args, **kwargs): with gr.Blocks() as demo: with gr.Row(): gr.Markdown(scale=2).attach_load_event(self.hello, None) gr.LoginButton() gr.LogoutButton() self.out = gr.Markdown() inp = gr.File(file_types=['.epub']) inp.change(self.process, inp, self.out) demo.launch() def process(self, file, profile: gr.OAuthProfile | None): if profile is None: return gr.update(value='Login to access the tool.') ch_list = epub2txt(file.name, outputlist=True) chapter_titles = epub2txt.content_titles title = epub2txt.title return gr.update( value=f"# {title}\n\n" + model.response(prompt+ch_list[2])) # value=f"# {title}\n\n" + "\n\n".join( # [f"## {ct}\n\n{c}" for ct, c in zip(chapter_titles, ch_list)])) def hello(self, profile: gr.OAuthProfile | None): if profile is None: return '# ePub summarization tool\n\nLogin to access the tool.' return f"# ePub summarization tool\n\nWelcome {profile.name}!!" GUI()