File size: 1,953 Bytes
9168672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45495af
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
import gradio as gr
from sentence_transformers import SentenceTransformer
import numpy as np

# Load the model
model = SentenceTransformer("MohJaf/bayan-usuli-bert")

def get_embeddings(text):
    """Get embeddings for input text"""
    if not text or not text.strip():
        return {"error": "No input text provided"}

    embeddings = model.encode(text)
    return {
        "query": text,
        "embeddings": embeddings.tolist(),
        "dimensions": len(embeddings)
    }

def compute_similarity(text1, text2):
    """Compute similarity between two texts"""
    if not text1 or not text2:
        return {"error": "Both texts are required"}

    embeddings = model.encode([text1, text2])
    similarity = float(np.dot(embeddings[0], embeddings[1]) /
                       (np.linalg.norm(embeddings[0]) * np.linalg.norm(embeddings[1])))

    return {
        "text1": text1,
        "text2": text2,
        "similarity": similarity
    }

# Create Gradio interface
with gr.Blocks(title="Bayan Usuli BERT API") as demo:
    gr.Markdown("# Bayan Usuli BERT - Arabic Islamic Jurisprudence Model")
    gr.Markdown("This API provides embeddings for Arabic texts related to Islamic jurisprudence (Usul al-Fiqh).")

    with gr.Tab("Get Embeddings"):
        text_input = gr.Textbox(label="Arabic Text", placeholder="Enter your text here...", rtl=True)
        embed_btn = gr.Button("Get Embeddings")
        embed_output = gr.JSON(label="Result")
        embed_btn.click(get_embeddings, inputs=text_input, outputs=embed_output)

    with gr.Tab("Compute Similarity"):
        text1 = gr.Textbox(label="First Text", placeholder="Enter first text...", rtl=True)
        text2 = gr.Textbox(label="Second Text", placeholder="Enter second text...", rtl=True)
        sim_btn = gr.Button("Compute Similarity")
        sim_output = gr.JSON(label="Result")
        sim_btn.click(compute_similarity, inputs=[text1, text2], outputs=sim_output)

demo.launch()