File size: 8,937 Bytes
04f132f
 
 
 
 
2b90e9b
04f132f
2e11edb
04f132f
2b90e9b
04f132f
 
 
 
2b90e9b
0df6ef6
 
2b90e9b
04f132f
 
 
 
2b90e9b
04f132f
 
2b90e9b
0df6ef6
04f132f
 
 
 
 
2b90e9b
 
04f132f
2b90e9b
 
 
04f132f
 
2b90e9b
04f132f
 
 
0df6ef6
2b90e9b
 
 
 
 
 
 
 
 
 
 
0df6ef6
04f132f
 
 
2b90e9b
 
 
 
 
 
 
04f132f
 
 
2b90e9b
c128c4e
2b90e9b
 
c128c4e
2b90e9b
 
c128c4e
 
2b90e9b
c128c4e
 
2b90e9b
 
e47bd95
2b90e9b
 
 
 
 
04f132f
2b90e9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04f132f
2b90e9b
 
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
from transformers import pipeline
import gradio as gr
import torch
import re

def get_native_feature_demo(language, input_description, target_feature, question, return_numbers=True):
    task = 'question-answering'
    french_model = 'AgentPublic/camembert-base-squadFR-fquad-piaf'
    english_model = 'deepset/roberta-base-squad2'
    
    if language == "French":
        model = pipeline(task, model=french_model, tokenizer=french_model)
    else:
        model = pipeline(task, model=english_model, tokenizer=english_model)
    
    # Updated pipeline call using keyword arguments
    answer = model(question=question, context=input_description)
    
    if target_feature == "price":
        reference_target_value = 1650
    else:
        reference_target_value = 85
        
    try:
        if return_numbers:
            nums = re.findall(r"\d*\.\d+|\d+\,\d+|\d{1,3}'[\d{3}']+\d{1,3}\.?\d*|(?<!m)\d+", answer['answer'])
            nums = [re.sub(r",|'", "", num) for num in nums]
            if len(nums) > 1:
                nums = [float(x) for x in nums]
                answer_processed = min(nums, key=lambda x:abs(x-reference_target_value))
            elif len(nums) == 1:
                answer_processed = float(''.join(nums))
            else:
                return "No numbers found!", None, None
        else:
            answer_processed = answer['answer']
            
        answer_processed = round(float(answer_processed), 2)
        answer_full = answer['answer']
        score = answer['score']
        return ('%f' % answer_processed).rstrip('0').rstrip('.'), f"{score:.4f}", answer_full
    except:
        return "No answer!", None, None

# Example data remains the same
french_example = """ Venez découvrir votre futur appartement lors des portes ouvertes Quai Ouest à Renens le samedi 6 mars de 9h00 à 12h00. Afin de respecter les mesures de sécurité, merci d'inscrire une personne par tranche horaire. Merci de laisser vos coordonnées (nom, email et téléphone) afin que l'on puisse vous joindre pour confirmer le rendez-vous et l'adresse. Lorsque vous entrer votre nom pour vous inscrire, merci d'indiquer également l'appartement que vous souhaitez visiter (n° ou nombre de pièces). En cas d'intérêt, merci de venir avec un dossier complet : - 3 dernières fiches de salaire - Extrait de l'office des poursuites - Copie carte d'identité ou permis de séjour - Copie de votre police d'assurance RC (responsabilité civile) et ménage Un formulaire de location sera à disposition sur place. Pour en savoir plus: https://quai-ouest.ch/location/logements\nINSCRIPTIONS : https://doodle.com/poll/5cvrqyt2ueaah3su?utm_source=poll&utm_medium=link\nNouvelle promotion de 89 logements en location idéalement située à la Gare de Renens, Quai Ouest ouvrira ses bras aux futurs locataires avec plusieurs options de logements, commerces et bureaux.\nCe magnifique appartement de 4.5 pièces se situe au 3° étage et a été construit avec de très beaux matériaux et se compose comme suit:\nSpacieux séjour de 40m2\nSuite parentale de 23.9m2 avec salle de douche italienne attenante\nDeux chambres à coucher d'environ 13m2 chacune\nRaccordement pour colonne de lavage\nUne salle de bains/WC\nCuisine ouverte avec îlot central et entièrement agencée avec lave-vaisselle\nStores électriques\nEntrée avec armoires murales\nLoyer net : CHF 2'600 .-\nCharges: CHF 320.-\nTotal: CHF 2'920.-\nAdresse: Place de la Gare à 1020 Renens\nPlus d'informations sur www.quai-ouest.ch/location/logements ainsi que chez Omnia Immobilier"""

english_example = """Come and discover your future apartment at the Quai Ouest open house in Renens on Saturday, March 6th, from 9:00 am to 12:00 pm. In order to respect the security measures, please register one person per hour. Please leave your contact details (name, email and telephone) so that you can be reached to confirm the appointment and address. When entering your name to register, please also indicate the apartment you wish to visit (no or number of rooms). In case of interest, please come with a complete file: - 3 last salary slips - Excerpt from the prosecution office - Copy of identity card or residence permit - Copy of your insurance policy RC (civil liability) and household A rental form will be available on site. For more information: https://quai-ouest.ch/rental/housing INSCRIPTIONS: https://doodle.com/poll/5cvrqyt2ueaah3su?utm_source=poll&utm_medium=link New promotion of 89 rental units ideally located at the Gare de Renens, several new housing options. This magnificent 4.5-room apartment is located on the 3rd floor and has been built with beautiful materials and consists as follows: Spacious living room of 40m2 Master suite of 23.9m2 with adjoining Italian shower room Two bedrooms of about 13m2 each Wash-column connection A bathroom/WC Open kitchen with central island and fully arranged with dishwasher Electric stores Entrance with wall cabinets Net rent: CHF 2'600 .- Charges: CHF 320.- Total: CHF 2'920.- Address: Place de la Gare à 1020 Renens More information on www.quai-ouest.ch/location/housing as well as at Omnia Immobilier"""

examples = [
    ["French", french_example, 'price', "Combien payez-vous de loyer?"],
    ["French", french_example, 'living_space', "Quelle est la surface en m2?"],
    ["English", english_example, 'price', "How much is the rent?"],
    ["English", english_example, 'living_space', "How large is the surface?"]
]

# Rest of the code remains the same
title = "Extract relevant features from Real Estate Descriptions!"

description = """
<div style="margin-bottom: 1.5rem;">
    <div style="display: flex; justify-content: center; margin-bottom: 1.5rem">
        <img src="https://teara.govt.nz/files/c-21363-atl.jpg" width="300px" alt="Real Estate Image" style="border-radius: 8px;"/>
    </div>
    <div style="text-align: justify">
    Features with missing instances can negatively impact the performance of machine learning models. Information Extraction (IE) can improve the availability of tabular data by identifying relevant information from unstructured textual descriptions. This project demonstrated the application of IE on descriptions of online real estate listings, whereby the required missing values are retrieved from the text. Inspired by question-answering tasks, the aim was to recover these values by asking a set of questions. We tested two ways to achieve this goal. The first one focuses on a model specific to the language of the description (French) to perform IE, while the second translates the descriptions into English before IE. The project compared the performance of both approaches while delivering insights on how the formulation of the questions can impact the effectiveness of Q&A models.
    </div>
</div>
"""

css = """
.examples-table {
    text-align: left !important;
}
.examples-table td {
    text-align: left !important;
}
.examples-table p {
    text-align: left !important;
}
.prose td {
    text-align: left !important;
}
"""

article = """
<div style="text-align: center; margin-top: 1.5rem;">
    Created by <a href="https://iliaazizi.netlify.app/" target="_blank" style="color: #2563eb;">Ilia Azizi</a> 2022
</div>
"""

# Create the Gradio interface
with gr.Blocks(title=title, css=css) as demo:
    gr.HTML(description)
    
    with gr.Row():
        with gr.Column():
            language = gr.Radio(
                choices=["French", "English"],
                label="Language",
                value="French"
            )
            target_feature = gr.Dropdown(
                choices=["price", "living_space"],
                label="Target Feature",
                value="price"
            )
            question = gr.Textbox(
                lines=2,
                label="Question",
                placeholder="Ask to retrieve a feature."
            )
            input_description = gr.Textbox(
                lines=10,
                label="Real Estate Description",
                placeholder="Type a real estate description here."
            )
            submit_btn = gr.Button("Extract Feature", variant="primary")
        
        with gr.Column():
            extracted_feature = gr.Textbox(label="Extracted Feature")
            prediction_prob = gr.Textbox(label="Probability of the prediction")
            full_answer = gr.Textbox(label="Full Q&A answer")
    
    gr.Examples(
        examples=examples,
        inputs=[language, input_description, target_feature, question],
        outputs=[extracted_feature, prediction_prob, full_answer],
        fn=get_native_feature_demo,
        cache_examples=True
    )
    
    gr.HTML(article)
    
    submit_btn.click(
        fn=get_native_feature_demo,
        inputs=[language, input_description, target_feature, question],
        outputs=[extracted_feature, prediction_prob, full_answer]
    )

if __name__ == "__main__":
    demo.launch()