File size: 1,357 Bytes
62d45fc
152cb8b
62d45fc
152cb8b
 
 
 
 
 
 
62d45fc
152cb8b
 
657a6ed
152cb8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from sentence_transformers import CrossEncoder

def rank_tasks(query, tasks_string):
 
    model = CrossEncoder("vaibhav-ceew/onet-msmacroL6")
    tasks = [task.strip() for task in tasks_string.split(',')]
    scores = model.predict([(query, task) for task in tasks])
    scored_tasks = list(zip(tasks, scores))
    sorted_tasks = sorted(scored_tasks, key=lambda x: x[1], reverse=True)

    output_string = ""
    for task, score in sorted_tasks:
        output_string += f"{task} ,{score:.4f}\n"
    return output_string

# Sample Query and Tasks
sample_query = "Which planet is known as the Red Planet?"
sample_tasks = "Venus is often called Earth's twin because of its similar size and proximity.,Mars, known for its reddish appearance, is often referred to as the Red Planet.,Jupiter, the largest planet in our solar system, has a prominent red spot.,Saturn, famous for its rings, is sometimes mistaken for the Red Planet."

demo = gr.Interface(
    fn=rank_tasks,
    inputs=[
        gr.Textbox(label="Enter your query"),
        gr.Textbox(label="Enter tasks separated by comma")
    ],
    outputs=gr.Textbox(label="Ranked Tasks"),
    title="Task Relevance Ranking",
    description="Enter a query and a comma-separated list of tasks to see their relevance ranking.",
    examples=[[sample_query, sample_tasks]]
)

demo.launch()