Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| # The API endpoint URL | |
| similarity_problem = 'http://34.166.48.42:5555/similarity_problem' | |
| similarity_solution = 'http://34.166.48.42:5555/similarity_solution' | |
| glob = {} | |
| import json | |
| import random | |
| import string | |
| def generate_random_string(length=10): | |
| letters = string.ascii_lowercase | |
| return ''.join(random.choice(letters) for i in range(length)) | |
| def generate_random_json_object(): | |
| return { | |
| "problem_link": f"https://example.com/problem/{generate_random_string()}", | |
| "solution": generate_random_string(20), | |
| "similarity": round(random.uniform(0, 1), 2) | |
| } | |
| def get_similar_problems(problem_statement, problem_solution, top_k): | |
| if not problem_statement: | |
| if not problem_solution: | |
| return "ERROR: Input the problem's statement and/or solution" | |
| # The JSON object to send (with one key-value pair) | |
| data = {'solution': problem_solution, | |
| 'top_k': top_k} | |
| # Send the POST request with the JSON data | |
| response = requests.post(similarity_solution, json=data) | |
| if response.status_code == 200: | |
| similar_solutions = response.json() | |
| # similar_solutions = [generate_random_json_object() for _ in range(10)] | |
| ret = {} | |
| glob.clear() | |
| for json_obj in similar_solutions: | |
| ret[json_obj.get("problem_link", "No problem link provided")] = json_obj.get("similarity", "No similarity provided") | |
| glob[json_obj.get("problem_link", "No problem link provided")] = json_obj.get("solution", "No problem solution provided") | |
| return ret | |
| else: | |
| glob.clear() | |
| return {"NaN" : 0} | |
| else: | |
| # The JSON object to send (with one key-value pair) | |
| data = {'problem': problem_statement, | |
| 'top_k': top_k} | |
| # Send the POST request with the JSON data | |
| response = requests.post(similarity_problem, json=data) | |
| if response.status_code == 200: | |
| similar_problems = response.json() | |
| ret = {} | |
| glob.clear() | |
| for json_obj in similar_problems: | |
| ret[json_obj.get("problem_link", "No problem link provided")] = json_obj.get("similarity", "No similarity provided") | |
| glob[json_obj.get("problem_link", "No problem link provided")] = json_obj.get("problem_statement", "No problem statement provided") | |
| else: | |
| glob.clear() | |
| return {"NaN" : 0} | |
| def show_problem(evt: gr.SelectData): # SelectData is a subclass of EventData | |
| uid = evt.value | |
| if uid != "NaN": | |
| statement = glob[uid] | |
| title = uid | |
| url = uid | |
| markdown = f"# [{title}]({url})\n\n" | |
| markdown += f"### Statement\n\n{statement}" | |
| return markdown | |
| with gr.Blocks( | |
| title="Similarity Module", css=".mymarkdown {font-size: 15px !important}" | |
| ) as demo: | |
| gr.Markdown( | |
| """ | |
| # Similarity Module | |
| IPC Intelligent Programming Companion | |
| """ | |
| ) | |
| with gr.Row(): | |
| # column for inputs | |
| with gr.Column(): | |
| problem_statement = gr.Textbox( | |
| label="Statement", | |
| info="Paste your statement here!", | |
| value="", | |
| ) | |
| problem_solution = gr.Textbox( | |
| label="Solution", | |
| info="Paste your solution here!", | |
| value="", | |
| ) | |
| topk_slider = gr.Slider( | |
| minimum=1, | |
| maximum=100, | |
| step=1, | |
| value=10, | |
| label="Number of similar problems to show", | |
| ) | |
| submit_button = gr.Button("Submit") | |
| my_markdown = gr.Markdown( | |
| latex_delimiters=[ | |
| {"left": "$$", "right": "$$", "display": True}, | |
| {"left": "$", "right": "$", "display": False}, | |
| {"left": "\\(", "right": "\\)", "display": False}, | |
| {"left": "\\[", "right": "\\]", "display": True}, | |
| ], | |
| elem_classes="mymarkdown", | |
| ) | |
| # column for outputs | |
| with gr.Column(): | |
| output_labels = gr.Label( | |
| label="Similar problems", | |
| ) | |
| submit_button.click( | |
| fn=get_similar_problems, | |
| inputs=[problem_statement, problem_solution, topk_slider], | |
| outputs=[output_labels], | |
| ) | |
| output_labels.select(fn=show_problem, inputs=None, outputs=[my_markdown]) | |
| demo.launch() |