File size: 4,434 Bytes
08cff1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9ef635
08cff1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import gradio as gr
from gpt4all import GPT4All
from huggingface_hub import hf_hub_download
import os

current_directory = os.getcwd()
model_directory = os.path.join(current_directory, "models")

title = "TaoScience"

description = """
<h1><center>LLM Finetuned on TaoScience<center></h1>
<h3><center>TaoGPT is a fine-tuned LLM on Tao Science by Dr. Rulin Xu and Dr. Zhi Gang Sha. <br> Check out- <a href='https://github.com/agencyxr/taogpt7B'>Github Repo</a> For More Information. πŸ’¬<h3><center>

"""

NOMIC = """
<!DOCTYPE html>
<html>
  <head>
    <title>TaoGPT - DataMap</title>
    <style>
      iframe {
        width: 100%;
        height: 600px; /* You can adjust the height as needed */
        border: 0;
      }
    </style>
  </head>
  <body>
    <iframe
      src="https://atlas.nomic.ai/map/c1ce06f4-7ed0-4c02-88a4-dd3b47bdf878/f2941fb8-0f36-4a23-8cbe-40dbf76ca9e4?xs=-41.09135&xf=41.12038&ys=-22.50394&yf=23.67273"
    ></iframe>
  </body>
</html>
"""

model_path = "models"
model_name = "taogpt-v1-gguf.Q5_K_M.gguf"
if os.path.exists(model_directory) and os.path.isdir(model_directory):
  print("Models folder already exits")
else: 
  hf_hub_download(repo_id="agency888/TaoGPT-v1-GGUF-GGUF", filename=model_name, local_dir=model_path, local_dir_use_symlinks=False)
  

print("Start the model init process")
model = model = GPT4All(model_name, model_path, allow_download = False, device="cpu")
print("Finish the model init process")

model.config["promptTemplate"] = """{0}
"""

model.config["systemPrompt"] = "In the Context of TaoScience answer this questions: "
model._is_chat_session_activated = False

max_new_tokens = 2048

def generator(message, history, temperature, top_p, top_k):
    prompt = ""
    for user_message, assistant_message in history:
        prompt += model.config["promptTemplate"].format(user_message)

    prompt += model.config["promptTemplate"].format(message)
    outputs = []    
    for token in model.generate(prompt=prompt, temp=temperature, top_k = top_k, top_p = top_p, max_tokens = max_new_tokens, streaming=True):
        outputs.append(token)
        yield "".join(outputs)

def vote(data: gr.LikeData):
    if data.liked:
        return
    else:
        return

chatbot = gr.Chatbot(bubble_full_width=False)

additional_inputs=[
    gr.Slider(
        label="temperature",
        value=0.2,
        minimum=0.0,
        maximum=2.0,
        step=0.05,
        interactive=True,
        info="Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.",
    ),
    gr.Slider(
        label="top_p",
        value=1.0,
        minimum=0.0,
        maximum=1.0,
        step=0.01,
        interactive=True,
        info="0.1 means only the tokens comprising the top 10% probability mass are considered. Suggest set to 1 and use temperature. 1 means 100% and will disable it",
    ),
    gr.Slider(
        label="top_k",
        value=40,
        minimum=0,
        maximum=1000,
        step=1,
        interactive=True,
        info="limits candidate tokens to a fixed number after sorting by probability. Setting it higher than the vocabulary size deactivates this limit.",
    )
]

with gr.Blocks() as demo:
    gr.HTML("<h1><center>TaoGPTv0<center></h1>")
    gr.HTML("<h3><center>TaoGPTv0 is a fine-tuned Mistal-7B model with a retrieval augmented generation pipeline on Tao Science by Dr. Rulin Xu and Dr. Zhi Gang Sha. Check out- <a href='https://github.com/agencyxr/taogpt7B'>Github Repo</a> For More Information. πŸ’¬<h3><center>")
    with gr.Column():
        with gr.Accordion("Visualise Training Data"):
            gr.HTML("<h3>Look into the dataset we used to finetune our model</h3>")
            gr.HTML(NOMIC)
        with gr.Column():
            gr.ChatInterface(
            fn = generator,
            title=title,
            description = description,
            chatbot=chatbot,
            additional_inputs=additional_inputs,
            examples=[
                ["What is TaoScience ?"],
                ["TaoScience was written by ?"],
                ["Tell me more about TaoScience"]],)
            RAG_Checkbox = gr.Checkbox(label="Use Retrival Augmented Generation" , value=True , interactive=False)  
            gr.Markdown("The model is prone to Hallucination and many not always be Factual")

if __name__ == "__main__":
    demo.queue(max_size=50).launch(share=True)