Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,89 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            from wordllama import WordLlama
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            # Load the default WordLlama model
         | 
| 5 | 
            +
            wl = WordLlama.load()
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            def calculate_similarity(sentence1, sentence2):
         | 
| 8 | 
            +
                similarity_score = wl.similarity(sentence1, sentence2)
         | 
| 9 | 
            +
                return similarity_score
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            def rank_documents(query, candidates):
         | 
| 12 | 
            +
                ranked_docs = wl.rank(query, candidates)
         | 
| 13 | 
            +
                return ranked_docs
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            def deduplicate_candidates(candidates, threshold):
         | 
| 16 | 
            +
                deduplicated = wl.deduplicate(candidates, threshold)
         | 
| 17 | 
            +
                return deduplicated
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            def filter_candidates(query, candidates, threshold):
         | 
| 20 | 
            +
                filtered = wl.filter(query, candidates, threshold)
         | 
| 21 | 
            +
                return filtered
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            def topk_candidates(query, candidates, k):
         | 
| 24 | 
            +
                topk = wl.topk(query, candidates, k)
         | 
| 25 | 
            +
                return topk
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            # Define the Gradio interface
         | 
| 28 | 
            +
            def create_gradio_interface():
         | 
| 29 | 
            +
                with gr.Blocks() as demo:
         | 
| 30 | 
            +
                    gr.Markdown("## WordLlama Gradio Demo")
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                    with gr.Tab("Similarity"):
         | 
| 33 | 
            +
                        with gr.Row():
         | 
| 34 | 
            +
                            sentence1 = gr.Textbox(label="Sentence 1", placeholder="Enter the first sentence here...")
         | 
| 35 | 
            +
                            sentence2 = gr.Textbox(label="Sentence 2", placeholder="Enter the second sentence here...")
         | 
| 36 | 
            +
                        similarity_output = gr.Number(label="Similarity Score")
         | 
| 37 | 
            +
                        gr.Button("Calculate Similarity").click(
         | 
| 38 | 
            +
                            fn=calculate_similarity,
         | 
| 39 | 
            +
                            inputs=[sentence1, sentence2],
         | 
| 40 | 
            +
                            outputs=[similarity_output]
         | 
| 41 | 
            +
                        )
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                    with gr.Tab("Rank Documents"):
         | 
| 44 | 
            +
                        query = gr.Textbox(label="Query", placeholder="Enter the query here...")
         | 
| 45 | 
            +
                        candidates = gr.Textbox(label="Candidates (comma separated)", placeholder="Enter candidate sentences here...")
         | 
| 46 | 
            +
                        ranked_docs_output = gr.Dataframe(headers=["Document", "Score"], datatype=["str", "float"])
         | 
| 47 | 
            +
                        gr.Button("Rank Documents").click(
         | 
| 48 | 
            +
                            fn=lambda q, c: rank_documents(q, c.split(',')),
         | 
| 49 | 
            +
                            inputs=[query, candidates],
         | 
| 50 | 
            +
                            outputs=[ranked_docs_output]
         | 
| 51 | 
            +
                        )
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                    with gr.Tab("Deduplicate Candidates"):
         | 
| 54 | 
            +
                        candidates_dedup = gr.Textbox(label="Candidates (comma separated)", placeholder="Enter candidate sentences here...")
         | 
| 55 | 
            +
                        threshold_dedup = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, step=0.01, value=0.8)
         | 
| 56 | 
            +
                        deduplicated_output = gr.Textbox(label="Deduplicated Candidates")
         | 
| 57 | 
            +
                        gr.Button("Deduplicate").click(
         | 
| 58 | 
            +
                            fn=lambda c, t: deduplicate_candidates(c.split(','), t),
         | 
| 59 | 
            +
                            inputs=[candidates_dedup, threshold_dedup],
         | 
| 60 | 
            +
                            outputs=[deduplicated_output]
         | 
| 61 | 
            +
                        )
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                    with gr.Tab("Filter Candidates"):
         | 
| 64 | 
            +
                        filter_query = gr.Textbox(label="Query", placeholder="Enter the query here...")
         | 
| 65 | 
            +
                        candidates_filter = gr.Textbox(label="Candidates (comma separated)", placeholder="Enter candidate sentences here...")
         | 
| 66 | 
            +
                        threshold_filter = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, step=0.01, value=0.3)
         | 
| 67 | 
            +
                        filtered_output = gr.Textbox(label="Filtered Candidates")
         | 
| 68 | 
            +
                        gr.Button("Filter Candidates").click(
         | 
| 69 | 
            +
                            fn=lambda q, c, t: filter_candidates(q, c.split(','), t),
         | 
| 70 | 
            +
                            inputs=[filter_query, candidates_filter, threshold_filter],
         | 
| 71 | 
            +
                            outputs=[filtered_output]
         | 
| 72 | 
            +
                        )
         | 
| 73 | 
            +
             | 
| 74 | 
            +
                    with gr.Tab("Top-k Candidates"):
         | 
| 75 | 
            +
                        topk_query = gr.Textbox(label="Query", placeholder="Enter the query here...")
         | 
| 76 | 
            +
                        candidates_topk = gr.Textbox(label="Candidates (comma separated)", placeholder="Enter candidate sentences here...")
         | 
| 77 | 
            +
                        k = gr.Slider(label="Top-k", minimum=1, maximum=10, step=1, value=3)
         | 
| 78 | 
            +
                        topk_output = gr.Textbox(label="Top-k Candidates")
         | 
| 79 | 
            +
                        gr.Button("Get Top-k Candidates").click(
         | 
| 80 | 
            +
                            fn=lambda q, c, k: topk_candidates(q, c.split(','), k),
         | 
| 81 | 
            +
                            inputs=[topk_query, candidates_topk, k],
         | 
| 82 | 
            +
                            outputs=[topk_output]
         | 
| 83 | 
            +
                        )
         | 
| 84 | 
            +
             | 
| 85 | 
            +
                return demo
         | 
| 86 | 
            +
             | 
| 87 | 
            +
            # Create and launch the Gradio interface
         | 
| 88 | 
            +
            demo = create_gradio_interface()
         | 
| 89 | 
            +
            demo.launch(debug = True)
         | 
