Spaces:
Running
Running
File size: 2,766 Bytes
21a73c4 | 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 | import gradio as gr
import requests
import json
ENV_URL = 'https://boobesh007-code-review.hf.space'
def review_code(code, language, difficulty):
try:
# Reset environment
reset_resp = requests.post(ENV_URL + '/reset', timeout=30)
obs = reset_resp.json()
# Create action based on code analysis
action = {
'has_syntax_error': ':' not in code and 'def ' in code,
'quality_score': 0.5,
'issues': ['code submitted for review'],
'severity': 'medium'
}
# Step environment
step_resp = requests.post(ENV_URL + '/step', json=action, timeout=30)
result = step_resp.json()
score = result.get('reward', 0.5)
feedback = result.get('feedback', 'Review complete')
task_type = result.get('task_type', difficulty)
# Format output
output = '## π Code Review Results\n\n'
output += f'**Language:** {language}\n\n'
output += f'**Difficulty:** {task_type}\n\n'
output += f'**Score:** {score:.2f}/1.00\n\n'
output += f'**Feedback:** {feedback}\n\n'
if score >= 0.8:
output += 'β
**Excellent code quality!**'
elif score >= 0.5:
output += 'β οΈ **Some issues found - review needed**'
else:
output += 'β **Critical issues found - immediate fix needed**'
return output
except Exception as e:
return f'Error: {str(e)}'
# Build UI
with gr.Blocks(title='AI Code Review', theme=gr.themes.Soft()) as demo:
gr.Markdown('# π» AI Code Review Environment')
gr.Markdown('Paste your code below and get instant AI-powered feedback!')
with gr.Row():
with gr.Column():
code_input = gr.Textbox(
label='π Your Code',
placeholder='Paste your code here...',
lines=10
)
language = gr.Dropdown(
choices=['python', 'javascript', 'java'],
value='python',
label='π Language'
)
difficulty = gr.Dropdown(
choices=['easy', 'medium', 'hard'],
value='easy',
label='π― Difficulty Level'
)
submit_btn = gr.Button('π Review Code', variant='primary')
with gr.Column():
output = gr.Markdown(label='Results')
submit_btn.click(
fn=review_code,
inputs=[code_input, language, difficulty],
outputs=output
)
gr.Markdown('---')
gr.Markdown('Built with β€οΈ using OpenEnv | [GitHub](https://github.com/boobesh789/Code-review)')
if __name__ == '__main__':
demo.launch(server_name='0.0.0.0', server_port=7860)
|