File size: 2,256 Bytes
b3f14a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from streamlit.logger import get_logger
from transformers import  pipeline
import datasets
import pandas as pd
from huggingface_hub import login

LOGGER = get_logger(__name__)
model = "sivan22/halacha-siman-seif-classifier-new"


login('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')
ds = datasets.load_dataset('sivan22/orach-chaim',token=True)
df = ds['train'].to_pandas()
def clean(s)->str:
    return s.replace(" ","")
df['seif']= df['seif'].apply(clean)



def get_predicts_local(input)->str:
    classifier = pipeline("text-classification",model=model,top_k=None)
    predicts = classifier(input)
    return predicts

def get_predicts_online(input)->str:
    import requests
    API_URL = "https://api-inference.huggingface.co/models/" + model
    headers = {"Authorization": f"Bearer {'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'}"}
    def query(input_text):
        response = requests.post(API_URL, headers=headers, json='{{inputs:' +input_text+'}{wait_for_model:true}}')        
        return response.json()
    predicts = query(input)
    return predicts

def run():
    st.set_page_config(
        page_title="Halacha classification",
        page_icon="",
    )

    st.write("# 讞讬驻讜砖 讘砖讜诇讞谉 注专讜讱")
    use_local = st.checkbox("讞讬驻讜砖 诇讗 诪拽讜讜谉")
    user_input = st.text_input('讻转讜讘 讻讗谉 讗转 砖讗诇转讱', placeholder='讻诪讛 谞专讜转 诪讚诇讬拽讬诐 讘讞谞讜讻讛')
    if st.button('讞驻砖') and user_input!="":       
        get_predicts = get_predicts_local if use_local else get_predicts_online
        #print(get_predicts(user_input)[0][0:5])
        for prediction in get_predicts(user_input)[0][:5]:
            rows = df[((df["bookname"] == " 砖诇讞谉 注专讜讱 - 讗讜专讞 讞讬讬诐 ") |
                        (df["bookname"] ==" 诪砖谞讛 讘专讜专讛")) &
                      (df["siman"] == prediction['label'].split(' ')[0])&
                      (df["seif"] == prediction['label'].split(' ')[1]) ]
            rows.sort_values(["bookname"],ascending=False, inplace=True) 
         
            print(prediction['label'].split(' '))
            st.write('住讬诪谉 ' + str(prediction['label']), rows[['text','sek','seif','siman','bookname']])

    

if __name__ == "__main__":
    run()