Spaces:
Running
Running
File size: 7,899 Bytes
750020e 11b325d 750020e 98fced7 750020e 4c91de3 4fa44c7 4c91de3 762f02d 4fa44c7 4c91de3 4fa44c7 4c91de3 4fa44c7 4c91de3 1995166 4fa44c7 1995166 4fa44c7 4c91de3 1af25ec ceed53e 1af25ec 4c91de3 9fcaecd 750020e fe3eace 033a22d 7a72935 033a22d 227499e 033a22d 227499e 033a22d 227499e 033a22d 227499e 033a22d 227499e 033a22d fe3eace 033a22d 589a379 033a22d 4c8a985 fe3eace 4c8a985 589a379 7a72935 9fcaecd b4f2e1a 9fcaecd 4c91de3 7a72935 4c91de3 f7b2a4e 4512bf7 4c91de3 589a379 7a72935 589a379 4405d10 7a72935 4405d10 7a72935 4c91de3 7a72935 750020e 4c91de3 05775e4 4c91de3 750020e 05775e4 750020e 05775e4 4c91de3 750020e a0503fe 841a86c a0503fe 589a379 7a72935 589a379 fe3eace 750020e 9e9c47f e03a3c9 589a379 98fced7 7a72935 fe3eace 750020e |
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
import transformers
import re
from transformers import AutoConfig, AutoTokenizer, AutoModel, AutoModelForCausalLM, pipeline
import torch
import gradio as gr
import json
import os
import shutil
import requests
import pandas as pd
# Define the device
device = "cuda" if torch.cuda.is_available() else "cpu"
editorial_model = "PleIAs/Bibliography-Formatter"
token_classifier = pipeline(
"token-classification", model=editorial_model, aggregation_strategy="simple", device=device
)
tokenizer = AutoTokenizer.from_pretrained(editorial_model, model_max_length=512)
css = """
<style>
.manuscript {
display: flex;
margin-bottom: 10px;
align-items: baseline;
}
.annotation {
width: 15%;
padding-right: 20px;
color: grey !important;
font-style: italic;
text-align: right;
}
.content {
width: 80%;
}
h2 {
margin: 0;
font-size: 1.5em;
}
.title-content h2 {
font-weight: bold;
}
.bibliography-content {
color:darkgreen !important;
margin-top: -5px; /* Adjust if needed to align with annotation */
}
.paratext-content {
color:#a4a4a4 !important;
margin-top: -5px; /* Adjust if needed to align with annotation */
}
</style>
"""
# Preprocess the 'word' column
def preprocess_text(text):
# Remove HTML tags
text = re.sub(r'<[^>]+>', '', text)
# Replace newlines with spaces
text = re.sub(r'\n', ' ', text)
# Replace multiple spaces with a single space
text = re.sub(r'\s+', ' ', text)
# Strip leading and trailing whitespace
return text.strip()
def split_text(text, max_tokens=500):
# Split the text by newline characters
parts = text.split("\n")
chunks = []
current_chunk = ""
for part in parts:
# Add part to current chunk
if current_chunk:
temp_chunk = current_chunk + "\n" + part
else:
temp_chunk = part
# Tokenize the temporary chunk
num_tokens = len(tokenizer.tokenize(temp_chunk))
if num_tokens <= max_tokens:
current_chunk = temp_chunk
else:
if current_chunk:
chunks.append(current_chunk)
current_chunk = part
if current_chunk:
chunks.append(current_chunk)
# If no newlines were found and still exceeding max_tokens, split further
if len(chunks) == 1 and len(tokenizer.tokenize(chunks[0])) > max_tokens:
long_text = chunks[0]
chunks = []
while len(tokenizer.tokenize(long_text)) > max_tokens:
split_point = len(long_text) // 2
while split_point < len(long_text) and not re.match(r'\s', long_text[split_point]):
split_point += 1
# Ensure split_point does not go out of range
if split_point >= len(long_text):
split_point = len(long_text) - 1
chunks.append(long_text[:split_point].strip())
long_text = long_text[split_point:].strip()
if long_text:
chunks.append(long_text)
return chunks
def remove_punctuation(text):
return re.sub(r'[^\w\s]', '', text)
def extract_year(text):
year_match = re.search(r'\b(\d{4})\b', text)
return year_match.group(1) if year_match else None
def create_bibtex_entry(data):
# Determine the entry type
if 'journal' in data:
entry_type = 'article'
elif 'booktitle' in data:
entry_type = 'chapter'
else:
entry_type = 'book'
# Extract year from 'None' if it exists
none_content = data.pop('none', '')
year = extract_year(none_content)
if year and 'year' not in data:
data['year'] = year
# Create BibTeX ID
author_words = data.get('author', '').split()
first_author = author_words[0] if author_words else 'Unknown'
bibtex_id = f"{first_author}{year}" if year else first_author
bibtex_id = remove_punctuation(bibtex_id.lower())
bibtex = f"@{entry_type}{{{bibtex_id},\n"
for key, value in data.items():
if value.strip():
if key in ['volume', 'year']:
value = remove_punctuation(value)
if key == 'pages':
value = value.replace('p. ', '')
bibtex += f" {key.lower()} = {{{value.strip()}}},\n"
bibtex = bibtex.rstrip(',\n') + "\n}"
return bibtex
def transform_chunks(marianne_segmentation):
marianne_segmentation = pd.DataFrame(marianne_segmentation)
marianne_segmentation = marianne_segmentation[marianne_segmentation['entity_group'] != 'separator']
marianne_segmentation['word'] = marianne_segmentation['word'].astype(str).str.replace('¶', '\n', regex=False)
marianne_segmentation['word'] = marianne_segmentation['word'].astype(str).apply(preprocess_text)
marianne_segmentation = marianne_segmentation[marianne_segmentation['word'].notna() & (marianne_segmentation['word'] != '') & (marianne_segmentation['word'] != ' ')]
html_output = []
bibtex_data = {}
current_entity = None
for _, row in marianne_segmentation.iterrows():
entity_group = row['entity_group']
result_entity = "[" + entity_group.capitalize() + "]"
word = row['word']
if entity_group != 'None':
if entity_group in bibtex_data:
bibtex_data[entity_group] += ' ' + word
else:
bibtex_data[entity_group] = word
current_entity = entity_group
else:
if current_entity:
bibtex_data[current_entity] += ' ' + word
else:
bibtex_data['None'] = bibtex_data.get('None', '') + ' ' + word
html_output.append(f'<div class="manuscript"><div class="annotation">{result_entity}</div><div class="content">{word}</div></div>')
bibtex_entry = create_bibtex_entry(bibtex_data)
final_html = '\n'.join(html_output)
return final_html, bibtex_entry
# Class to encapsulate the Falcon chatbot
class MistralChatBot:
def __init__(self, system_prompt="Le dialogue suivant est une conversation"):
self.system_prompt = system_prompt
def predict(self, user_message):
editorial_text = re.sub("\n", " ¶ ", user_message)
num_tokens = len(tokenizer.tokenize(editorial_text))
if num_tokens > 500:
batch_prompts = split_text(editorial_text, max_tokens=500)
else:
batch_prompts = [editorial_text]
out = token_classifier(batch_prompts)
classified_list = []
for classification in out:
df = pd.DataFrame(classification)
classified_list.append(df)
classified_list = pd.concat(classified_list)
# Debugging: Print the classified list
print("Classified List:")
print(classified_list)
html_output, bibtex_entry = transform_chunks(classified_list)
# Debugging: Print the outputs
print("HTML Output:")
print(html_output)
print("BibTeX Entry:")
print(bibtex_entry)
return bibtex_entry
# Create the Falcon chatbot instance
mistral_bot = MistralChatBot()
# Define the Gradio interface
title = "Éditorialisation"
description = "Un outil expérimental d'identification de la structure du texte à partir d'un encoder (Deberta)"
examples = [
[
"Qui peut bénéficier de l'AIP?", # user_message
0.7 # temperature
]
]
demo = gr.Blocks()
with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
gr.HTML("""<h1 style="text-align:center">Reversed Zotero</h1>""")
text_input = gr.Textbox(label="Your text", type="text", lines=5)
text_button = gr.Button("Extract a structured bibtex")
bibtex_output = gr.Textbox(label="BibTeX Entry", lines=10)
text_button.click(mistral_bot.predict, inputs=text_input, outputs=[bibtex_output])
if __name__ == "__main__":
demo.queue().launch() |