Spaces:
Runtime error
Runtime error
| import os | |
| import requests | |
| import gradio as gr | |
| # ENV vars | |
| API_URL = os.environ["API_URL"] | |
| HF_TOKEN = os.environ["HF_TOKEN"] | |
| headers = {"Authorization": f"Bearer {HF_TOKEN}"} | |
| langs = ['C', 'C++', 'Java', 'Golang', 'Rust', | |
| 'Javascript', 'PHP', 'Kotlin', 'HTML', 'Python', 'Bash', 'Ruby'] | |
| jsn_trail = {"parameters": | |
| { | |
| "top_p": 0.9, | |
| "max_new_tokens": 64, | |
| "return_full_text": True, | |
| "do_sample": True, | |
| }, | |
| "options": | |
| {"use_cache": True, | |
| "wait_for_model": True, | |
| }, } | |
| def post(jsn): | |
| response = requests.post(API_URL, headers=headers, json=jsn) | |
| return response.json()[0]["generated_text"] | |
| def divider(char='=', length=50): | |
| return '\n' + char*length + '\n' | |
| def get_solution(lang, error): | |
| jsn = {"inputs": "**Programming Language**:\n" + lang + divider() + | |
| '**Error**:\n'+error+divider()+"**Solution**:\n", **jsn_trail} | |
| return post(jsn) | |
| def feedback(opt): | |
| return post({"inputs": opt, **jsn_trail}) | |
| demo = gr.Blocks() | |
| with demo: | |
| gr.Markdown( | |
| "<h1><center>Paste your error to see solution</center></h1>") | |
| with gr.Row(): | |
| dropdown = gr.Dropdown(value="Python", | |
| choices=langs, label="Choose the language") | |
| # with gr.Column: | |
| error = gr.Textbox(label="Write the error output", | |
| value="Traceback (most recent call last):\nFile \" < stdin > \", line 1, in <module>\nNameError: name 'variable' is not defined", lines=6) | |
| with gr.Row(): | |
| generated_txt = gr.Textbox(lines=5, interactive=False, label="Output") | |
| btn = gr.Button("Solve") | |
| btn.click(get_solution, inputs=[dropdown, | |
| error], outputs=generated_txt) | |
| feeedback_btn = gr.Button("Feedback") | |
| feeedback_btn.click( | |
| feedback, inputs=[generated_txt], outputs=generated_txt) | |
| with gr.Row(): | |
| gr.Markdown( | |
| "") | |
| demo.launch(enable_queue=True, debug=True) | |