Spaces:
Sleeping
Sleeping
Pclanglais
commited on
Commit
•
bc16047
1
Parent(s):
5322356
Upload 5 files
Browse files- README.md +7 -6
- app.py +182 -0
- gitattributes +4 -0
- requirements.txt +14 -0
- theme_builder.py +3 -0
README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
---
|
2 |
-
title: Code
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Code demo
|
3 |
+
emoji: 📜
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: blue
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.50.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: mit
|
11 |
---
|
12 |
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import transformers
|
2 |
+
import numpy as np
|
3 |
+
import re
|
4 |
+
from transformers import AutoConfig, AutoTokenizer, AutoModel, AutoModelForCausalLM
|
5 |
+
from vllm import LLM, SamplingParams
|
6 |
+
import torch
|
7 |
+
import json
|
8 |
+
import os
|
9 |
+
import shutil
|
10 |
+
import requests
|
11 |
+
from pprint import pprint
|
12 |
+
import chromadb
|
13 |
+
import pandas as pd
|
14 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
15 |
+
from FlagEmbedding import BGEM3FlagModel
|
16 |
+
|
17 |
+
pd.set_option('display.max_columns', None)
|
18 |
+
|
19 |
+
#sampling_params = SamplingParams(temperature=.7, top_p=.95, max_tokens=2000, presence_penalty = 1.5, stop = ["``"])
|
20 |
+
|
21 |
+
# Define the device
|
22 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
23 |
+
#Define variables
|
24 |
+
temperature=0.2
|
25 |
+
max_new_tokens=1000
|
26 |
+
top_p=0.92
|
27 |
+
repetition_penalty=1.7
|
28 |
+
|
29 |
+
model_name = "Inagua/code-model"
|
30 |
+
|
31 |
+
llm = LLM(model_name, max_model_len=4096)
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
#CSS for references formatting
|
36 |
+
css = """
|
37 |
+
.generation {
|
38 |
+
margin-left:2em;
|
39 |
+
margin-right:2em;
|
40 |
+
}
|
41 |
+
|
42 |
+
:target {
|
43 |
+
background-color: #CCF3DF; /* Change the text color to red */
|
44 |
+
}
|
45 |
+
|
46 |
+
.source {
|
47 |
+
float:left;
|
48 |
+
max-width:17%;
|
49 |
+
margin-left:2%;
|
50 |
+
}
|
51 |
+
|
52 |
+
.tooltip {
|
53 |
+
position: relative;
|
54 |
+
cursor: pointer;
|
55 |
+
font-variant-position: super;
|
56 |
+
color: #97999b;
|
57 |
+
}
|
58 |
+
|
59 |
+
.tooltip:hover::after {
|
60 |
+
content: attr(data-text);
|
61 |
+
position: absolute;
|
62 |
+
left: 0;
|
63 |
+
top: 120%; /* Adjust this value as needed to control the vertical spacing between the text and the tooltip */
|
64 |
+
white-space: pre-wrap; /* Allows the text to wrap */
|
65 |
+
width: 500px; /* Sets a fixed maximum width for the tooltip */
|
66 |
+
max-width: 500px; /* Ensures the tooltip does not exceed the maximum width */
|
67 |
+
z-index: 1;
|
68 |
+
background-color: #f9f9f9;
|
69 |
+
color: #000;
|
70 |
+
border: 1px solid #ddd;
|
71 |
+
border-radius: 5px;
|
72 |
+
padding: 5px;
|
73 |
+
display: block;
|
74 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1); /* Optional: Adds a subtle shadow for better visibility */
|
75 |
+
}"""
|
76 |
+
|
77 |
+
#Curtesy of chatgpt
|
78 |
+
def format_references(text):
|
79 |
+
# Define start and end markers for the reference
|
80 |
+
ref_start_marker = '<ref text="'
|
81 |
+
ref_end_marker = '</ref>'
|
82 |
+
|
83 |
+
# Initialize an empty list to hold parts of the text
|
84 |
+
parts = []
|
85 |
+
current_pos = 0
|
86 |
+
ref_number = 1
|
87 |
+
|
88 |
+
# Loop until no more reference start markers are found
|
89 |
+
while True:
|
90 |
+
start_pos = text.find(ref_start_marker, current_pos)
|
91 |
+
if start_pos == -1:
|
92 |
+
# No more references found, add the rest of the text
|
93 |
+
parts.append(text[current_pos:])
|
94 |
+
break
|
95 |
+
|
96 |
+
# Add text up to the start of the reference
|
97 |
+
parts.append(text[current_pos:start_pos])
|
98 |
+
|
99 |
+
# Find the end of the reference text attribute
|
100 |
+
end_pos = text.find('">', start_pos)
|
101 |
+
if end_pos == -1:
|
102 |
+
# Malformed reference, break to avoid infinite loop
|
103 |
+
break
|
104 |
+
|
105 |
+
# Extract the reference text
|
106 |
+
ref_text = text[start_pos + len(ref_start_marker):end_pos].replace('\n', ' ').strip()
|
107 |
+
ref_text_encoded = ref_text.replace("&", "&").replace("<", "<").replace(">", ">")
|
108 |
+
|
109 |
+
# Find the end of the reference tag
|
110 |
+
ref_end_pos = text.find(ref_end_marker, end_pos)
|
111 |
+
if ref_end_pos == -1:
|
112 |
+
# Malformed reference, break to avoid infinite loop
|
113 |
+
break
|
114 |
+
|
115 |
+
# Extract the reference ID
|
116 |
+
ref_id = text[end_pos + 2:ref_end_pos].strip()
|
117 |
+
|
118 |
+
# Create the HTML for the tooltip
|
119 |
+
tooltip_html = f'<span class="tooltip" data-refid="{ref_id}" data-text="{ref_id}: {ref_text_encoded}"><a href="#{ref_id}">[' + str(ref_number) +']</a></span>'
|
120 |
+
parts.append(tooltip_html)
|
121 |
+
|
122 |
+
# Update current_pos to the end of the current reference
|
123 |
+
current_pos = ref_end_pos + len(ref_end_marker)
|
124 |
+
ref_number = ref_number + 1
|
125 |
+
|
126 |
+
# Join and return the parts
|
127 |
+
parts = ''.join(parts)
|
128 |
+
|
129 |
+
return parts
|
130 |
+
|
131 |
+
# Class to encapsulate the Falcon chatbot
|
132 |
+
class MistralChatBot:
|
133 |
+
def __init__(self, system_prompt="Le dialogue suivant est une conversation"):
|
134 |
+
self.system_prompt = system_prompt
|
135 |
+
|
136 |
+
def predict(self, user_message, context):
|
137 |
+
detailed_prompt = """### Question ###\n""" + user_message + "\n\n### Contexte ###\n" + context + "\n\n### Formule ###\n"
|
138 |
+
prompts = [detailed_prompt]
|
139 |
+
outputs = llm.generate(prompts, sampling_params, use_tqdm = False)
|
140 |
+
generated_text = outputs[0].outputs[0].text
|
141 |
+
generated_text = '<h2 style="text-align:center">Réponse</h3>\n<div class="generation">' + generated_text + "</div>"
|
142 |
+
fiches_html = ""
|
143 |
+
return generated_text, fiches_html
|
144 |
+
|
145 |
+
# Create the Falcon chatbot instance
|
146 |
+
mistral_bot = MistralChatBot()
|
147 |
+
|
148 |
+
# Define the Gradio interface
|
149 |
+
title = "Inagua"
|
150 |
+
description = "An experimental LLM to interact with DAMAaaS documentation"
|
151 |
+
examples = [
|
152 |
+
[
|
153 |
+
"How to calculate a linear regression?", # user_message
|
154 |
+
0.7 # temperature
|
155 |
+
]
|
156 |
+
]
|
157 |
+
|
158 |
+
additional_inputs=[
|
159 |
+
gr.Slider(
|
160 |
+
label="Température",
|
161 |
+
value=0.2, # Default value
|
162 |
+
minimum=0.05,
|
163 |
+
maximum=1.0,
|
164 |
+
step=0.05,
|
165 |
+
interactive=True,
|
166 |
+
info="Des valeurs plus élevées donne plus de créativité, mais aussi d'étrangeté",
|
167 |
+
),
|
168 |
+
]
|
169 |
+
|
170 |
+
|
171 |
+
demo = gr.Blocks()
|
172 |
+
|
173 |
+
with gr.Blocks(theme='gradio/monochrome', css=css) as demo:
|
174 |
+
gr.HTML("""<h1 style="text-align:center">SkikitLLM</h1>""")
|
175 |
+
text_input = gr.Textbox(label="Your question", type="text", lines=1)
|
176 |
+
context_input = gr.Textbox(label="Your context", type="text", lines=1)
|
177 |
+
text_button = gr.Button("Query SkikitLLM")
|
178 |
+
text_output = gr.HTML(label="Answer")
|
179 |
+
text_button.click(mistral_bot.predict, inputs=[text_input, context_input], outputs=[text_output])
|
180 |
+
|
181 |
+
if __name__ == "__main__":
|
182 |
+
demo.queue().launch()
|
gitattributes
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
education_corrected/chroma.sqlite3 filter=lfs diff=lfs merge=lfs -text
|
2 |
+
education_corrected/e150eb41-e894-45c4-b97c-80ced9ff2123/data_level0.bin filter=lfs diff=lfs merge=lfs -text
|
3 |
+
education_corrected/a9ac8f33-9498-450a-ae99-f116efb66330/data_level0.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
education_corrected/6af97eb5-0cfa-40b2-a4df-732ca13bd66a/data_level0.bin filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
einops
|
4 |
+
accelerate
|
5 |
+
tiktoken
|
6 |
+
scipy
|
7 |
+
transformers_stream_generator==0.0.4
|
8 |
+
peft
|
9 |
+
deepspeed
|
10 |
+
bitsandbytes
|
11 |
+
optimum
|
12 |
+
vllm==0.3.2
|
13 |
+
chromadb
|
14 |
+
sentence_transformers
|
theme_builder.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
gr.themes.builder()
|