File size: 2,703 Bytes
605193c
 
 
 
 
 
f85e000
 
605193c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f85e000
605193c
 
 
 
 
e112ee7
605193c
 
 
86fc1ec
605193c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e112ee7
605193c
 
 
 
 
 
 
 
e112ee7
605193c
 
 
e112ee7
605193c
 
e112ee7
605193c
 
 
86fc1ec
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import requests
import os

import gradio as gr

ML_ENDPOINT_URL = os.environ.get("ML_ENDPOINT_URL", "http://50.18.255.74:8040/rewrite")
client_session = requests.Session()
client_session.keep_alive = 5

# Example queries to help users understand the app
EXAMPLE_QUERIES = [
    [
        "In what year was the winner of the 44th edition of the Miss World competition born?"
    ],
    ["Who lived longer, Nikola Tesla or Milutin Milankovic?"],
    [
        "Author David Chanoff has collaborated with a U.S. Navy admiral who served as the ambassador to the United Kingdom under which President?"
    ],
    ["Create a table for top noise cancelling headphones that are not expensive"],
    ["what are some ways to do fast query reformulation?"],
]


def make_request(query):
    try:
        # Replace with your actual API endpoint
        if "rewrite: " not in query:
            query = f"rewrite: {query}"
        response = client_session.post(ML_ENDPOINT_URL, json={"inputs": query})
        response.raise_for_status()
        result = response.json()
    except requests.exceptions.RequestException as e:
        result = f"Error: {str(e)}"

    return result


# Create the Gradio interface
with gr.Blocks() as app:
    gr.Markdown(
        """
    # Query Reformulation Assistant

    This tool helps you rewrite text in different semantically style. Simply enter your text and it will be rewritten according to the prefix:
    The prefix "rewrite:" will be automatically added if not present.
    """
    )

    with gr.Row():
        query_input = gr.Textbox(
            label="Enter your text to rewrite",
            placeholder="Type your text here, or try one of the examples below...",
            lines=3,
        )

    with gr.Row():
        submit_btn = gr.Button("Submit", variant="primary")
        clear_btn = gr.Button("Clear")

    with gr.Row():
        response_output = gr.Textbox(label="Rewritten Text", lines=5, interactive=False)

    # Add examples section
    gr.Examples(
        examples=EXAMPLE_QUERIES,
        inputs=query_input,
        outputs=[response_output],
        fn=make_request,
        cache_examples=True,
        label="Example Queries",
    )

    # Clear button functionality
    clear_btn.click(
        lambda: ("", "", ""),  # Clear input  # Clear output  # Clear time label
        outputs=[query_input, response_output],
    )

    # Submit button click event
    submit_btn.click(fn=make_request, inputs=[query_input], outputs=[response_output])

    # Add keyboard shortcut for submission
    query_input.submit(fn=make_request, inputs=[query_input], outputs=[response_output])

# Launch the app
if __name__ == "__main__":
    app.launch()