Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,168 @@
|
|
1 |
-
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
HF_Hub_API_token = os.environ.get('HF_Hub_API_token', None)
|
5 |
-
import gradio as gr
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#import os
|
2 |
+
#import gradio as gr
|
3 |
+
|
4 |
+
#HF_Hub_API_token = os.environ.get('HF_Hub_API_token', None)
|
5 |
+
#import gradio as gr
|
6 |
+
|
7 |
+
#demo = gr.load("gaelhuser/patent_gen_prv", hf_token=HF_Hub_API_token, src="spaces")
|
8 |
+
#demo.launch()
|
9 |
+
|
10 |
import gradio as gr
|
11 |
|
12 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
13 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
14 |
+
from langchain.chains import ConversationChain
|
15 |
+
from langchain.memory import ConversationBufferMemory
|
16 |
+
from langchain.memory import ConversationBufferWindowMemory
|
17 |
+
|
18 |
+
import os
|
19 |
HF_Hub_API_token = os.environ.get('HF_Hub_API_token', None)
|
|
|
20 |
|
21 |
+
llm = HuggingFaceEndpoint(
|
22 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
23 |
+
task="text-generation",
|
24 |
+
max_new_tokens=2048,
|
25 |
+
do_sample=False,
|
26 |
+
repetition_penalty=1.03,
|
27 |
+
huggingfacehub_api_token=HF_Hub_API_token
|
28 |
+
)
|
29 |
+
|
30 |
+
memory = ConversationBufferMemory()
|
31 |
+
conversation = ConversationChain(llm=llm, verbose=False, memory=memory)
|
32 |
+
|
33 |
+
from prompts import *
|
34 |
+
|
35 |
+
patent_draft = [] # shared between all users !!
|
36 |
+
trim = False#True
|
37 |
+
fraction = .15
|
38 |
+
|
39 |
+
def trim_output(response):
|
40 |
+
trimmed_output = ''
|
41 |
+
n_paragraphs = len(response.split('\n'))
|
42 |
+
if n_paragraphs > 0:
|
43 |
+
trimmed_output += response.split('\n')[0]
|
44 |
+
if n_paragraphs > 1:
|
45 |
+
other_paragraphs = response.split('\n')[1:]
|
46 |
+
others = []
|
47 |
+
for parag in other_paragraphs:
|
48 |
+
if len(parag.split(' ')) > 1:
|
49 |
+
n_words = len(parag.split(' '))
|
50 |
+
split = int(fraction*n_words)
|
51 |
+
remaining = n_words - split
|
52 |
+
others.append(' '.join(parag.split(' ')[:split]) + f" (...) [ {remaining} words left ]")
|
53 |
+
trimmed_output += '\n' + '\n'.join(others)
|
54 |
+
return trimmed_output
|
55 |
+
|
56 |
+
def gen_intro(invention_title, progress=gr.Progress()):
|
57 |
+
# si on a pas déjà cliqué sur ce bouton
|
58 |
+
if sum(['** Introduction: **' in p for p in patent_draft]) == 0:
|
59 |
+
prompt1 = prompt1a + invention_title + prompt1b
|
60 |
+
for i in progress.tqdm(range(1)):
|
61 |
+
if len(invention_title) > 0:
|
62 |
+
response = conversation.predict(input = prompt1)
|
63 |
+
if trim:
|
64 |
+
response = trim_output(response)
|
65 |
+
patent_draft.append("** Introduction: **\n" + response)
|
66 |
+
return '\n'.join(patent_draft)
|
67 |
+
# sinon retourne ce qui est déjà affiché
|
68 |
+
else: return '\n'.join(patent_draft)
|
69 |
+
|
70 |
+
def gen_bckgnd(current_sota, disadvantage, current_objective, progress=gr.Progress()):
|
71 |
+
if sum(['** Background: **' in p for p in patent_draft]) == 0:
|
72 |
+
prompt2 = prompt2a + current_sota + prompt2b + disadvantage + prompt2c + current_objective + prompt2_end
|
73 |
+
for i in progress.tqdm(range(1)):
|
74 |
+
if ((len(current_sota)) > 0 or (len(disadvantage) > 0) or len(current_objective) > 0):
|
75 |
+
response = conversation.predict(input = prompt2)
|
76 |
+
if trim:
|
77 |
+
response = trim_output(response)
|
78 |
+
patent_draft.append("\n ** Background: **\n" + response)
|
79 |
+
return '\n'.join(patent_draft)
|
80 |
+
else: return '\n'.join(patent_draft)
|
81 |
+
|
82 |
+
def gen_claim1(claim1, tech_adv, progress=gr.Progress()):
|
83 |
+
if sum(['** Technical description: **' in p for p in patent_draft]) == 0:
|
84 |
+
prompt3 = prompt3a + claim1 + prompt3b + tech_adv + prompt3c
|
85 |
+
for i in progress.tqdm(range(1)):
|
86 |
+
if ((len(claim1)) > 0 or (len(tech_adv) > 0)):
|
87 |
+
response = conversation.predict(input = prompt3)
|
88 |
+
if trim:
|
89 |
+
response = trim_output(response)
|
90 |
+
patent_draft.append("\n ** Technical description: **\n" + response)
|
91 |
+
return '\n'.join(patent_draft)
|
92 |
+
else: return '\n'.join(patent_draft)
|
93 |
+
|
94 |
+
def gen_dept_claims(dependent_claim, tech_effects, progress=gr.Progress()):
|
95 |
+
if sum(['** Technical description for dependent claims: **' in p for p in patent_draft]) == 0:
|
96 |
+
prompt4 = prompt4a + dependent_claim + prompt4b + tech_effects
|
97 |
+
for i in progress.tqdm(range(1)):
|
98 |
+
if len(dependent_claim) > 0 :
|
99 |
+
response = conversation.predict(input = prompt4)
|
100 |
+
if trim:
|
101 |
+
response = trim_output(response)
|
102 |
+
patent_draft.append("\n ** Technical description for dependent claims: **\n" + response)
|
103 |
+
return '\n'.join(patent_draft)
|
104 |
+
return '\n'.join(patent_draft)
|
105 |
+
else: return '\n'.join(patent_draft)
|
106 |
+
|
107 |
+
|
108 |
+
out = gr.Textbox(label="Patent draft:", show_label=True)
|
109 |
+
|
110 |
+
def clear():
|
111 |
+
global patent_draft
|
112 |
+
patent_draft = []
|
113 |
+
conversation.memory.clear()
|
114 |
+
return [None, None, None, None, None, None, None, None, None]
|
115 |
+
|
116 |
+
with gr.Blocks() as demo:
|
117 |
+
gr.Markdown(
|
118 |
+
"""<center><h2>Générateur de dépôt de brevet</center></h2>
|
119 |
+
<h3>Notice d'utilisation:</h3>""")
|
120 |
+
gr.Markdown(
|
121 |
+
"""
|
122 |
+
<br>- Pour chaque onglet "Introduction", "Background"... renseigner les champs (au besoin cliquer sur les exemples proposés en dessous de chaque zone de texte)
|
123 |
+
<br>- Valider chaque étape avec les boutons "Add introduction", "Add background", etc. A chaque validation, le champ "Patent draft" est mis à jour.
|
124 |
+
<br>- Le bouton "Reset draft" efface toutes les données saisies et remet la conversation à zero.<br>
|
125 |
+
""")
|
126 |
+
with gr.Row():
|
127 |
+
with gr.Tab("Introduction"):
|
128 |
+
invention_title = gr.Textbox(label="Enter invention title", show_label=True)
|
129 |
+
examples=gr.Examples(examples=[example_title], inputs=[invention_title])
|
130 |
+
btn = gr.Button("Add introduction")
|
131 |
+
btn.click(fn=gen_intro, inputs=[invention_title], outputs=out)
|
132 |
+
|
133 |
+
with gr.Tab("Background"):
|
134 |
+
current_sota = gr.Textbox(label="The current state of the art is the following:", show_label=True)
|
135 |
+
examples=gr.Examples(examples=[example_SOTA], inputs=[current_sota])
|
136 |
+
current_disadvantage = gr.Textbox(label="The disadvantage of the current state of the art is the following:", show_label=True)
|
137 |
+
examples=gr.Examples(examples=[example_disadvantage], inputs=[current_disadvantage])
|
138 |
+
current_objective = gr.Textbox(label="The Objective is the following::", show_label=True)
|
139 |
+
examples=gr.Examples(examples=[example_objective], inputs=[current_objective])
|
140 |
+
btn = gr.Button("Add invention background")
|
141 |
+
btn.click(fn=gen_bckgnd, inputs=[current_sota, current_disadvantage, current_objective], outputs=out)
|
142 |
+
|
143 |
+
with gr.Tab("Principal claim"):
|
144 |
+
principal_claim = gr.Textbox(label="The principal claim is the following:", show_label=True)
|
145 |
+
examples=gr.Examples(examples=[example_claim1], inputs=[principal_claim])
|
146 |
+
tech_adv = gr.Textbox(label="The technical advantage is the following:", show_label=True)
|
147 |
+
examples=gr.Examples(examples=[example_tech_adv], inputs=[tech_adv])
|
148 |
+
btn = gr.Button("Add description from principal claim")
|
149 |
+
btn.click(fn=gen_claim1, inputs=[principal_claim, tech_adv], outputs=out)
|
150 |
+
|
151 |
+
with gr.Tab("Dependent claim"):
|
152 |
+
dependent_claim = gr.Textbox(label="The dependent claims are the following:", show_label=True)
|
153 |
+
examples=gr.Examples(examples=[example_dep_claim], inputs=[dependent_claim])
|
154 |
+
tech_effects = gr.Textbox(label="Technical effects accepted by EPO:", show_label=True)
|
155 |
+
examples=gr.Examples(examples=[example_technical_effects], inputs=[tech_effects])
|
156 |
+
btn = gr.Button("Add description from dependent claims")
|
157 |
+
btn.click(fn=gen_dept_claims, inputs=[dependent_claim, tech_effects], outputs=out)
|
158 |
+
|
159 |
+
with gr.Row():
|
160 |
+
out.render()
|
161 |
+
with gr.Row():
|
162 |
+
#gr.ClearButton(components=[out])
|
163 |
+
clear_btn = gr.Button(value="Reset draft")
|
164 |
+
clear_btn.click(clear, inputs=[], outputs=[out, invention_title, current_sota, current_disadvantage, current_objective,
|
165 |
+
principal_claim, tech_adv, dependent_claim, tech_effects])
|
166 |
+
|
167 |
+
demo.queue().launch()
|
168 |
+
|