SteveTran commited on
Commit
605193c
·
verified ·
1 Parent(s): 3c81335

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import time
3
+ import os
4
+
5
+ import gradio as gr
6
+
7
+ ML_ENDPOINT_URL = os.environ.get("ML_ENDPOINT_URL", "http://50.18.255.74:8040/rewrite")
8
+
9
+ # Example queries to help users understand the app
10
+ EXAMPLE_QUERIES = [
11
+ [
12
+ "In what year was the winner of the 44th edition of the Miss World competition born?"
13
+ ],
14
+ ["Who lived longer, Nikola Tesla or Milutin Milankovic?"],
15
+ [
16
+ "Author David Chanoff has collaborated with a U.S. Navy admiral who served as the ambassador to the United Kingdom under which President?"
17
+ ],
18
+ ["Create a table for top noise cancelling headphones that are not expensive"],
19
+ ["what are some ways to do fast query reformulation?"],
20
+ ]
21
+
22
+
23
+ def make_request(query):
24
+ start_time = time.time()
25
+
26
+ try:
27
+ # Replace with your actual API endpoint
28
+ if "rewrite: " not in query:
29
+ query = f"rewrite: {query}"
30
+ response = requests.post(ML_ENDPOINT_URL, json={"inputs": query})
31
+ response.raise_for_status()
32
+ result = response.json()
33
+ except requests.exceptions.RequestException as e:
34
+ result = f"Error: {str(e)}"
35
+
36
+ end_time = time.time()
37
+ processing_time = round(end_time - start_time, 2)
38
+
39
+ return result, f"Response Time: {processing_time} seconds"
40
+
41
+
42
+ # Create the Gradio interface
43
+ with gr.Blocks() as demo:
44
+ gr.Markdown(
45
+ """
46
+ # Query Reformulation Assistant
47
+
48
+ This tool helps you rewrite text in different semantically style. Simply enter your text and it will be rewritten according to the prefix:
49
+ The prefix "rewrite:" will be automatically added if not present.
50
+ """
51
+ )
52
+
53
+ with gr.Row():
54
+ query_input = gr.Textbox(
55
+ label="Enter your text to rewrite",
56
+ placeholder="Type your text here, or try one of the examples below...",
57
+ lines=3,
58
+ )
59
+
60
+ with gr.Row():
61
+ submit_btn = gr.Button("Submit", variant="primary")
62
+ clear_btn = gr.Button("Clear")
63
+
64
+ with gr.Row():
65
+ response_output = gr.Textbox(label="Rewritten Text", lines=5, interactive=False)
66
+ time_label = gr.Label(label="Processing Time")
67
+
68
+ # Add examples section
69
+ gr.Examples(
70
+ examples=EXAMPLE_QUERIES,
71
+ inputs=query_input,
72
+ outputs=[response_output, time_label],
73
+ fn=make_request,
74
+ cache_examples=True,
75
+ label="Example Queries",
76
+ )
77
+
78
+ # Clear button functionality
79
+ clear_btn.click(
80
+ lambda: ("", "", ""), # Clear input # Clear output # Clear time label
81
+ outputs=[query_input, response_output, time_label],
82
+ )
83
+
84
+ # Submit button click event
85
+ submit_btn.click(
86
+ fn=make_request, inputs=[query_input], outputs=[response_output, time_label]
87
+ )
88
+
89
+ # Add keyboard shortcut for submission
90
+ query_input.submit(
91
+ fn=make_request, inputs=[query_input], outputs=[response_output, time_label]
92
+ )
93
+
94
+ # Launch the app
95
+ if __name__ == "__main__":
96
+ demo.launch(share=False, server_name="0.0.0.0", server_port=7860, favicon_path=None)