Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from sentence_transformers.cross_encoder import CrossEncoder
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Load the model
|
6 |
+
# Replace "ALJIACHI/Mizan-Rerank-v1" with your actual model name
|
7 |
+
model = CrossEncoder("ALJIACHI/Mizan-Rerank-v1")
|
8 |
+
|
9 |
+
def rerank_texts(query, texts):
|
10 |
+
"""
|
11 |
+
Rerank a list of texts based on their relevance to the query.
|
12 |
+
|
13 |
+
Args:
|
14 |
+
query (str): The search query
|
15 |
+
texts (str): Newline-separated texts to rank
|
16 |
+
|
17 |
+
Returns:
|
18 |
+
str: Formatted results with scores
|
19 |
+
"""
|
20 |
+
# Split the input texts by newline
|
21 |
+
text_list = [t.strip() for t in texts.split('\n') if t.strip()]
|
22 |
+
|
23 |
+
# Create sentence pairs
|
24 |
+
sentence_pairs = [[query, text] for text in text_list]
|
25 |
+
|
26 |
+
# Get scores from the model
|
27 |
+
scores = model.predict(sentence_pairs)
|
28 |
+
|
29 |
+
# Create results with ranks, scores, and texts
|
30 |
+
results = [(score, text) for score, text in zip(scores, text_list)]
|
31 |
+
|
32 |
+
# Sort by score in descending order
|
33 |
+
results.sort(reverse=True)
|
34 |
+
|
35 |
+
# Format the output
|
36 |
+
output = ""
|
37 |
+
for i, (score, text) in enumerate(results, 1):
|
38 |
+
output += f"#{i} (Score: {score:.4f}): {text}\n\n"
|
39 |
+
|
40 |
+
return output
|
41 |
+
|
42 |
+
# Create Gradio interface
|
43 |
+
demo = gr.Interface(
|
44 |
+
fn=rerank_texts,
|
45 |
+
inputs=[
|
46 |
+
gr.Textbox(label="Query", placeholder="Enter your search query here..."),
|
47 |
+
gr.Textbox(
|
48 |
+
label="Texts to Rank",
|
49 |
+
placeholder="Enter texts to rank, one per line...",
|
50 |
+
lines=10
|
51 |
+
)
|
52 |
+
],
|
53 |
+
outputs=gr.Textbox(label="Ranked Results"),
|
54 |
+
title="Arabic Text Reranking Demo",
|
55 |
+
description=(
|
56 |
+
"This demo uses the Mizan-Rerank-v1 model to rank texts based on their relevance to a query. "
|
57 |
+
"Enter your query and a list of texts (one per line), and the model will rank them by relevance."
|
58 |
+
),
|
59 |
+
examples=[
|
60 |
+
[
|
61 |
+
"ما هو القانون الجديد بشأن الضرائب في 2024؟",
|
62 |
+
"نشرت الجريدة الرسمية قانوناً جديداً في 2024 ينص على زيادة الضرائب على الشركات الكبرى بنسبة 5%\nالضرائب تعد مصدراً مهماً للدخل القومي وتختلف نسبتها من دولة إلى أخرى.\nافتتحت الحكومة مشروعاً جديداً للطاقة المتجددة في 2024."
|
63 |
+
],
|
64 |
+
[
|
65 |
+
"ما هو تفسير الآية وجعلنا من الماء كل شيء حي",
|
66 |
+
"تعني الآية أن الماء هو عنصر أساسي في حياة جميع الكائنات الحية، وهو ضروري لاستمرار الحياة.\nتم اكتشاف كواكب خارج المجموعة الشمسية تحتوي على مياه متجمدة.\nتحدث القرآن الكريم عن البرق والرعد في عدة مواضع مختلفة."
|
67 |
+
],
|
68 |
+
[
|
69 |
+
"ما هي فوائد فيتامين د؟",
|
70 |
+
"يساعد فيتامين د في تعزيز صحة العظام وتقوية الجهاز المناعي، كما يلعب دوراً مهماً في امتصاص الكالسيوم.\nيستخدم فيتامين د في بعض الصناعات الغذائية كمادة حافظة.\nيمكن الحصول على فيتامين د من خلال التعرض لأشعة الشمس أو تناول مكملات غذائية."
|
71 |
+
]
|
72 |
+
],
|
73 |
+
allow_flagging="never"
|
74 |
+
)
|
75 |
+
|
76 |
+
# Launch the demo
|
77 |
+
if __name__ == "__main__":
|
78 |
+
demo.launch()
|