Spaces:
Sleeping
Sleeping
mohammadjaf61-ops commited on
Commit ·
9168672
0
Parent(s):
Initial space setup
Browse files- README.md +22 -0
- app.py +53 -0
- requirements.txt +4 -0
README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Bayan Usuli BERT API
|
| 3 |
+
emoji: 📚
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# Bayan Usuli BERT API
|
| 14 |
+
|
| 15 |
+
Arabic Islamic Jurisprudence (Usul al-Fiqh) Embedding Model API.
|
| 16 |
+
|
| 17 |
+
This space provides an API for the `MohJaf/bayan-usuli-bert` sentence-transformers model.
|
| 18 |
+
|
| 19 |
+
## Features
|
| 20 |
+
|
| 21 |
+
- Get text embeddings for Arabic jurisprudence texts
|
| 22 |
+
- Compute semantic similarity between texts
|
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load the model
|
| 6 |
+
model = SentenceTransformer("MohJaf/bayan-usuli-bert")
|
| 7 |
+
|
| 8 |
+
def get_embeddings(text):
|
| 9 |
+
"""Get embeddings for input text"""
|
| 10 |
+
if not text or not text.strip():
|
| 11 |
+
return {"error": "No input text provided"}
|
| 12 |
+
|
| 13 |
+
embeddings = model.encode(text)
|
| 14 |
+
return {
|
| 15 |
+
"query": text,
|
| 16 |
+
"embeddings": embeddings.tolist(),
|
| 17 |
+
"dimensions": len(embeddings)
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
def compute_similarity(text1, text2):
|
| 21 |
+
"""Compute similarity between two texts"""
|
| 22 |
+
if not text1 or not text2:
|
| 23 |
+
return {"error": "Both texts are required"}
|
| 24 |
+
|
| 25 |
+
embeddings = model.encode([text1, text2])
|
| 26 |
+
similarity = float(np.dot(embeddings[0], embeddings[1]) /
|
| 27 |
+
(np.linalg.norm(embeddings[0]) * np.linalg.norm(embeddings[1])))
|
| 28 |
+
|
| 29 |
+
return {
|
| 30 |
+
"text1": text1,
|
| 31 |
+
"text2": text2,
|
| 32 |
+
"similarity": similarity
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
# Create Gradio interface
|
| 36 |
+
with gr.Blocks(title="Bayan Usuli BERT API") as demo:
|
| 37 |
+
gr.Markdown("# Bayan Usuli BERT - Arabic Islamic Jurisprudence Model")
|
| 38 |
+
gr.Markdown("This API provides embeddings for Arabic texts related to Islamic jurisprudence (Usul al-Fiqh).")
|
| 39 |
+
|
| 40 |
+
with gr.Tab("Get Embeddings"):
|
| 41 |
+
text_input = gr.Textbox(label="Arabic Text", placeholder="Enter your text here...", rtl=True)
|
| 42 |
+
embed_btn = gr.Button("Get Embeddings")
|
| 43 |
+
embed_output = gr.JSON(label="Result")
|
| 44 |
+
embed_btn.click(get_embeddings, inputs=text_input, outputs=embed_output)
|
| 45 |
+
|
| 46 |
+
with gr.Tab("Compute Similarity"):
|
| 47 |
+
text1 = gr.Textbox(label="First Text", placeholder="Enter first text...", rtl=True)
|
| 48 |
+
text2 = gr.Textbox(label="Second Text", placeholder="Enter second text...", rtl=True)
|
| 49 |
+
sim_btn = gr.Button("Compute Similarity")
|
| 50 |
+
sim_output = gr.JSON(label="Result")
|
| 51 |
+
sim_btn.click(compute_similarity, inputs=[text1, text2], outputs=sim_output)
|
| 52 |
+
|
| 53 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
sentence-transformers>=2.2.0
|
| 3 |
+
torch
|
| 4 |
+
numpy
|