epochs-demos commited on
Commit
25575eb
1 Parent(s): e3b0f8a

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py CHANGED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ import streamlit as st
4
+ from streamlit_ace import st_ace
5
+ import pandas as pd
6
+
7
+ button_style = st.markdown('''
8
+ <style>
9
+ div.stButton > button:first-child {{
10
+ background-color: #3A7EF1;
11
+ }}
12
+ </style>''', unsafe_allow_html=True)
13
+
14
+ left_co, cent_co,last_co = st.columns(3)
15
+ with cent_co:
16
+ st.image("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTzYyVPVc8EX7_N2QOh9D86t54SS7ucBDcOU_Tn52LwBi8BQTwcdeCVKcyjSKBGExjB4g&usqp=CAU")
17
+ st.markdown("<h5 style='color: #707379; text-align: center;'>School of AI by 1001epochs</h5>", unsafe_allow_html=True)
18
+ st.markdown('')
19
+
20
+
21
+ def check_solution(code, func_params):
22
+ try:
23
+ exec(code)
24
+ globals_dict = {{}}
25
+ exec(code, globals_dict)
26
+ function_to_test = globals_dict.get(func_params.get('function_name'))
27
+
28
+ if not callable(function_to_test):
29
+ raise ValueError(f"{{func_params.get('function_name')}} is not a callable function.")
30
+
31
+ # Run the tests and populate the results table
32
+ expected_result_list = []
33
+ actual_result_list = []
34
+ test_result = []
35
+ for input_params, expected_output in func_params["test_cases"]:
36
+ expected_result_list.append(expected_output)
37
+ result = function_to_test(*input_params)
38
+ actual_result_list.append(result)
39
+ test_result.append("Passed" if expected_result_list == actual_result_list else "Failed")
40
+ result_df = pd.DataFrame({
41
+ "Expected": expected_result_list,
42
+ "Run": actual_result_list,
43
+ "Result": test_result
44
+ }, index=None)
45
+ st.markdown(result_df.style.hide(axis="index").to_html(), unsafe_allow_html=True)
46
+ st.markdown("<br>", unsafe_allow_html=True)
47
+
48
+ # Check if all tests passed
49
+ if expected_result_list == actual_result_list:
50
+ st.success("All tests passed!")
51
+ else:
52
+ st.error("Some tests failed.")
53
+ return True
54
+
55
+ except SyntaxError as e:
56
+ st.error(f"Error: {{e}}")
57
+
58
+
59
+ def main():
60
+
61
+ with st.form('app'):
62
+ st.markdown("<h2 style='color: #707379;'>Coding Challenge - Code Practice</h2>", unsafe_allow_html=True)
63
+
64
+ # Description of the challenge
65
+ st.write("Create a function sum that meets the following criteria:")
66
+ st.write('''Sum of two values''')
67
+
68
+ # Display sample example for all test cases
69
+ sample_examples = "
70
+ ".join([f"sum({{params}}) -> {{expected_output}}" for params, expected_output in [((1, 2), 3), ((1, 0), 1), ((-1, -2), -3)]])
71
+ st.code(sample_examples, language="python")
72
+ show_solution_button = st.form_submit_button("Show Solution")
73
+ if show_solution_button:
74
+ solution = '''def sum(a,b):
75
+ return a+b '''
76
+ if solution:
77
+ st.code('''def sum(a,b):
78
+ return a+b ''', language="python")
79
+ else:
80
+ st.error('No Solution Provided!')
81
+
82
+ # Code input area
83
+ code = st_ace(value='''def sum(a,b):''', language="python", key="my_editor", theme='clouds', height=200)
84
+ # Check solution button
85
+ if st.form_submit_button("Check Solution"):
86
+ if code:
87
+ # Check the solution
88
+ check_solution(code, {
89
+ "function_name": "sum",
90
+ "test_cases": [((1, 2), 3), ((1, 0), 1), ((-1, -2), -3)]
91
+ })
92
+ else:
93
+ st.error("Please provide a solution.")
94
+
95
+ st.markdown("Copyright© by 1001epochs. All rights reserved.")
96
+
97
+ if __name__ == "__main__":
98
+ main()
99
+
100
+