AnonymousSub/minigpt4.cpp
0
1import os2import sys3import ctypes4import pathlib5from typing import Optional, List6import enum7from pathlib import Path8import argparse9import gradio as gr10 11import minigpt4_library12 13from huggingface_hub import hf_hub_download14 15model_path = hf_hub_download(repo_id='maknee/minigpt4-13b-ggml', filename='minigpt4-13B-f16.bin', repo_type='dataset')16llm_model_path = hf_hub_download(repo_id='maknee/ggml-vicuna-v0-quantized', filename='ggml-vicuna-13B-v0-q5_k.bin', repo_type='dataset')17 18title = """<h1 align="center">MiniGPT-4.cpp Demo</h1>"""19description = """<h3>This is the demo of MiniGPT-4 with ggml (cpu only!). Upload your images and start chatting!</h3>"""20article = """<div style='display:flex; gap: 0.25rem; '><a href='https://github.com/Vision-CAIR/MiniGPT-4'><img src='https://img.shields.io/badge/Github-Code-blue'></a></div>"""21 22global minigpt4_chatbot23minigpt4_chatbot: minigpt4_library.MiniGPT4ChatBot24 25def user(message, history):26 history = history or []27 # Append the user's message to the conversation history28 history.append([message, ""])29 return "", history30 31def chat(history, limit: int = 1024, temp: float = 0.8, top_k: int = 40, top_p: float = 0.9, repeat_penalty: float = 1.1):32 history = history or []33 34 message = history[-1][0]35 36 history[-1][1] = ""37 for output in minigpt4_chatbot.generate(38 message,39 limit = int(limit),40 temp = float(temp),41 top_k = int(top_k),42 top_p = float(top_p),43 ):44 answer = output45 history[-1][1] += answer46 # stream the response47 yield history, history48 49def clear_state(history, chat_message, image):50 history = []51 minigpt4_chatbot.reset_chat()52 return history, gr.update(value=None, interactive=True), gr.update(placeholder='Upload image first', interactive=False), gr.update(value="Upload & Start Chat", interactive=True)53 54def upload_image(image, history):55 if image is None:56 return None, None, gr.update(interactive=True), history57 history = []58 minigpt4_chatbot.upload_image(image.convert('RGB'))59 return gr.update(interactive=False), gr.update(interactive=True, placeholder='Type and press Enter'), gr.update(value="Start Chatting", interactive=False), history60 61def start():62 with gr.Blocks() as demo:63 gr.Markdown(title)64 gr.Markdown(description)65 gr.Markdown(article)66 67 with gr.Row():68 with gr.Column(scale=0.5):69 image = gr.Image(type="pil")70 upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")71 72 max_tokens = gr.Slider(1, 1024, label="Max Tokens", step=1, value=128)73 temperature = gr.Slider(0.0, 1.0, label="Temperature", step=0.05, value=0.8)74 top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.95)75 top_k = gr.Slider(0, 100, label="Top K", step=1, value=40)76 repeat_penalty = gr.Slider(0.0, 2.0, label="Repetition Penalty", step=0.1, value=1.1)77 78 with gr.Column():79 chatbot = gr.Chatbot(label='MiniGPT-4')80 message = gr.Textbox(label='User', placeholder='Upload image first', interactive=False)81 history = gr.State()82 83 with gr.Row():84 submit = gr.Button(value="Send message", variant="secondary").style(full_width=True)85 clear = gr.Button(value="Reset", variant="secondary").style(full_width=False)86 # stop = gr.Button(value="Stop", variant="secondary").style(full_width=False)87 88 clear.click(clear_state, inputs=[history, image, message], outputs=[history, image, message, upload_button], queue=False)89 90 upload_button.click(upload_image, inputs=[image, history], outputs=[image, message, upload_button, history])91 92 submit_click_event = submit.click(93 fn=user, inputs=[message, history], outputs=[message, history], queue=True94 ).then(95 fn=chat, inputs=[history, max_tokens, temperature, top_p, top_k, repeat_penalty], outputs=[chatbot, history], queue=True96 )97 message_submit_event = message.submit(98 fn=user, inputs=[message, history], outputs=[message, history], queue=True99 ).then(100 fn=chat, inputs=[history, max_tokens, temperature, top_p, top_k, repeat_penalty], outputs=[chatbot, history], queue=True101 )102 # stop.click(fn=None, inputs=None, outputs=None, cancels=[submit_click_event, message_submit_event], queue=False)103 104 demo.launch(enable_queue=True)105 106minigpt4_chatbot = minigpt4_library.MiniGPT4ChatBot(model_path, llm_model_path, verbosity=minigpt4_library.Verbosity.SILENT)107start()108 