ALJIACHI commited on
Commit
2fb6bb4
·
verified ·
1 Parent(s): 7a8267e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -2,59 +2,59 @@ 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
  [
@@ -73,6 +73,6 @@ demo = gr.Interface(
73
  allow_flagging="never"
74
  )
75
 
76
- # Launch the demo
77
  if __name__ == "__main__":
78
  demo.launch()
 
2
  from sentence_transformers.cross_encoder import CrossEncoder
3
  import numpy as np
4
 
5
+ # تحميل النموذج
6
+ # استبدل "ALJIACHI/Mizan-Rerank-v1" باسم النموذج الفعلي الخاص بك
7
  model = CrossEncoder("ALJIACHI/Mizan-Rerank-v1")
8
 
9
  def rerank_texts(query, texts):
10
  """
11
+ إعادة ترتيب قائمة من النصوص بناءً على مدى صلتها بالاستعلام.
12
 
13
+ المعاملات:
14
+ query (str): استعلام البحث
15
+ texts (str): نصوص مفصولة بسطر جديد للترتيب
16
 
17
+ العوائد:
18
+ str: النتائج المنسقة مع الدرجات
19
  """
20
+ # تقسيم النصوص المدخلة حسب السطر الجديد
21
  text_list = [t.strip() for t in texts.split('\n') if t.strip()]
22
 
23
+ # إنشاء أزواج الجمل
24
  sentence_pairs = [[query, text] for text in text_list]
25
 
26
+ # الحصول على الدرجات من النموذج
27
  scores = model.predict(sentence_pairs)
28
 
29
+ # إنشاء النتائج مع الترتيب والدرجات والنصوص
30
  results = [(score, text) for score, text in zip(scores, text_list)]
31
 
32
+ # الترتيب حسب الدرجة بترتيب تنازلي
33
  results.sort(reverse=True)
34
 
35
+ # تنسيق المخرجات
36
  output = ""
37
  for i, (score, text) in enumerate(results, 1):
38
+ output += f"#{i} (الدرجة: {score:.4f}): {text}\n\n"
39
 
40
  return output
41
 
42
+ # إنشاء واجهة Gradio
43
  demo = gr.Interface(
44
  fn=rerank_texts,
45
  inputs=[
46
+ gr.Textbox(label="الاستعلام", placeholder="أدخل استعلام البحث هنا..."),
47
  gr.Textbox(
48
+ label="النصوص المراد ترتيبها",
49
+ placeholder="أدخل النصوص المراد ترتيبها، نص واحد في كل سطر...",
50
  lines=10
51
  )
52
  ],
53
+ outputs=gr.Textbox(label="النتائج المرتبة"),
54
+ title="تطبيق إعادة ترتيب النصوص العربية",
55
  description=(
56
+ "يستخدم هذا التطبيق نموذج Mizan-Rerank-v1 لترتيب النصوص بناءً على مدى صلتها بالاستعلام. "
57
+ "أدخل استعلامك وقائمة من النصوص (نص واحد في كل سطر)، وسيقوم النموذج بترتيبها حسب الصلة."
58
  ),
59
  examples=[
60
  [
 
73
  allow_flagging="never"
74
  )
75
 
76
+ # تشغيل التطبيق
77
  if __name__ == "__main__":
78
  demo.launch()