Spaces:
Running
Running
File size: 3,982 Bytes
0dfb412 f2019a4 0dfb412 f2019a4 0dfb412 fab23a2 f2019a4 0dfb412 31559f1 0dfb412 208476f 0dfb412 22b51ff 0dfb412 22b51ff b053d03 0dfb412 bafe915 0dfb412 bafe915 0dfb412 bafe915 0dfb412 bafe915 0dfb412 bafe915 0dfb412 bafe915 0dfb412 bafe915 0dfb412 bafe915 0dfb412 bafe915 0dfb412 3481362 f2019a4 0dfb412 f2019a4 b053d03 0dfb412 7f1183e 0dfb412 b053d03 0dfb412 7468778 0dfb412 |
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 |
import transformers
import re
from transformers import AutoConfig, AutoTokenizer, AutoModel, AutoModelForCausalLM
from vllm import LLM, SamplingParams
import torch
import gradio as gr
import json
import os
import shutil
import requests
import chromadb
import difflib
import pandas as pd
from chromadb.config import Settings
from chromadb.utils import embedding_functions
# Define the device
device = "cuda" if torch.cuda.is_available() else "cpu"
model_name = "PleIAs/OCRonos"
llm = LLM(model_name, max_model_len=8128)
#CSS for references formatting
css = """
.generation {
margin-left:2em;
margin-right:2em;
size:1.2em;
}
:target {
background-color: #CCF3DF;
}
.source {
float:left;
max-width:17%;
margin-left:2%;
}
.tooltip {
position: relative;
cursor: pointer;
font-variant-position: super;
color: #97999b;
}
.tooltip:hover::after {
content: attr(data-text);
position: absolute;
left: 0;
top: 120%;
white-space: pre-wrap;
width: 500px;
max-width: 500px;
z-index: 1;
background-color: #f9f9f9;
color: #000;
border: 1px solid #ddd;
border-radius: 5px;
padding: 5px;
display: block;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
/* New styles for diff */
.deleted {
background-color: #ffcccb;
text-decoration: line-through;
}
.inserted {
background-color: #90EE90;
}
"""
#Curtesy of claude
def generate_html_diff(old_text, new_text):
d = difflib.Differ()
diff = list(d.compare(old_text.split(), new_text.split()))
html_diff = []
for word in diff:
if word.startswith(' '):
html_diff.append(word[2:])
elif word.startswith('- '):
html_diff.append(f'<span style="background-color: #ffcccb; text-decoration: line-through;">{word[2:]}</span>')
elif word.startswith('+ '):
html_diff.append(f'<span style="background-color: #90EE90;">{word[2:]}</span>')
return ' '.join(html_diff)
# 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):
sampling_params = SamplingParams(temperature=0.9, top_p=0.95, max_tokens=4000, presence_penalty=0, stop=["#END#"])
detailed_prompt = f"### TEXT ###\n{user_message}\n\n### CORRECTION ###\n"
print(detailed_prompt)
prompts = [detailed_prompt]
outputs = llm.generate(prompts, sampling_params, use_tqdm=False)
generated_text = outputs[0].outputs[0].text
# Generate HTML diff
html_diff = generate_html_diff(user_message, generated_text)
generated_text = '<h2 style="text-align:center">Réponse</h3>\n<div class="generation">' + html_diff + "</div>"
return generated_text
# Create the Falcon chatbot instance
mistral_bot = MistralChatBot()
# Define the Gradio interface
title = "Correction d'OCR"
description = "Un outil expérimental de correction d'OCR basé sur des modèles de langue"
examples = [
[
"Qui peut bénéficier de l'AIP?", # user_message
0.7 # temperature
]
]
additional_inputs=[
gr.Slider(
label="Température",
value=0.2, # Default value
minimum=0.05,
maximum=1.0,
step=0.05,
interactive=True,
info="Des valeurs plus élevées donne plus de créativité, mais aussi d'étrangeté",
),
]
demo = gr.Blocks()
with gr.Blocks(theme='JohnSmith9982/small_and_pretty', css=css) as demo:
gr.HTML("""<h1 style="text-align:center">Correction d'OCR</h1>""")
text_input = gr.Textbox(label="Votre texte.", type="text", lines=1)
text_button = gr.Button("Corriger l'OCR")
text_output = gr.HTML(label="Le texte corrigé")
text_button.click(mistral_bot.predict, inputs=text_input, outputs=[text_output])
if __name__ == "__main__":
demo.queue().launch() |