Cran-May/Shi-CI.Extensional.Analyzer
1
1from __future__ import annotations2import gradio as gr3import time4from ctransformers import AutoModelForCausalLM5from typing import Iterable6import gradio as gr7from gradio.themes.base import Base8from gradio.themes.utils import colors, fonts, sizes9import subprocess10 11from huggingface_hub import hf_hub_download12 13# Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.14model = AutoModelForCausalLM.from_pretrained("TheBloke/dolphin-2.0-mistral-7B-GGUF", model_file="dolphin-2.0-mistral-7b.Q3_K_L.gguf", model_type="mistral", gpu_layers=0)15ins = '''[INST] <<FRIDAY>>16Remember your name is "Shi-Ci" in English or “兮辞” in Chinese.You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.17If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.18<</FRIDAY>>19{} [/INST]20'''21 22 23theme = gr.themes.Monochrome(24 primary_hue="indigo",25 secondary_hue="blue",26 neutral_hue="slate",27 radius_size=gr.themes.sizes.radius_sm,28 font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],29)30def response(question):31 res = model(ins.format(question))32 yield res33 34 35examples = [36 "Hello!"37]38 39def process_example(args):40 for x in response(args):41 pass42 return x43 44css = ".generating {visibility: hidden}"45 46# Based on the gradio theming guide and borrowed from https://huggingface.co/spaces/shivi/dolly-v2-demo47class SeafoamCustom(Base):48 def __init__(49 self,50 *,51 primary_hue: colors.Color | str = colors.emerald,52 secondary_hue: colors.Color | str = colors.blue,53 neutral_hue: colors.Color | str = colors.blue,54 spacing_size: sizes.Size | str = sizes.spacing_md,55 radius_size: sizes.Size | str = sizes.radius_md,56 font: fonts.Font57 | str58 | Iterable[fonts.Font | str] = (59 fonts.GoogleFont("Quicksand"),60 "ui-sans-serif",61 "sans-serif",62 ),63 font_mono: fonts.Font64 | str65 | Iterable[fonts.Font | str] = (66 fonts.GoogleFont("IBM Plex Mono"),67 "ui-monospace",68 "monospace",69 ),70 ):71 super().__init__(72 primary_hue=primary_hue,73 secondary_hue=secondary_hue,74 neutral_hue=neutral_hue,75 spacing_size=spacing_size,76 radius_size=radius_size,77 font=font,78 font_mono=font_mono,79 )80 super().set(81 button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",82 button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",83 button_primary_text_color="white",84 button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",85 block_shadow="*shadow_drop_lg",86 button_shadow="*shadow_drop_lg",87 input_background_fill="zinc",88 input_border_color="*secondary_300",89 input_shadow="*shadow_drop",90 input_shadow_focus="*shadow_drop_lg",91 )92 93 94seafoam = SeafoamCustom()95 96 97with gr.Blocks(theme=seafoam, analytics_enabled=False, css=css) as demo:98 with gr.Column():99 gr.Markdown(100 """ ## Shi-Ci Extensional Analyzer101 102 Type in the box below and click the button to generate answers to your most pressing questions!103 104 """105 )106 107 with gr.Row():108 109 with gr.Column(scale=3):110 instruction = gr.Textbox(placeholder="Enter your question here", label="Question", elem_id="q-input")111 112 with gr.Box():113 gr.Markdown("**Answer**")114 output = gr.Markdown(elem_id="q-output")115 submit = gr.Button("Generate", variant="primary")116 gr.Examples(117 examples=examples,118 inputs=[instruction],119 cache_examples=True,120 fn=process_example,121 outputs=[output],122 )123 124 125 126 submit.click(response, inputs=[instruction], outputs=[output])127 instruction.submit(response, inputs=[instruction], outputs=[output])128 129demo.queue(concurrency_count=1).launch(debug=False,share=True)