File size: 3,475 Bytes
4d3ef13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import gradio as gr
import os
from langchain_chroma.vectorstores import Chroma
from langchain_huggingface.embeddings import (
    HuggingFaceEmbeddings,
    HuggingFaceEndpointEmbeddings,
)
import json
from convert_to_json import data_to_json

from template import make_template_outputs, make_template_testcases

if os.environ.get("HUGGINGFACEHUB_API_TOKEN"):
    embedding = HuggingFaceEndpointEmbeddings(
        repo_id="sentence-transformers/all-MiniLM-L6-v2",
        huggingfacehub_api_token=os.environ["HUGGINGFACEHUB_API_TOKEN"],
    )
else:
    embedding = HuggingFaceEmbeddings(
        model_name="sentence-transformers/all-MiniLM-L6-v2"
    )



count = 0


def increment_count():
    global count
    count += 1
    return count


def print_n_value(n_value):
    global no_tests
    no_tests = n_value
    return n_value  # Return the value if needed for further processing


def submit_second_page(topic):
    db_store = Chroma(collection_name="python-questions", persist_directory="./chroma", embedding_function=embedding)
    questions = db_store.similarity_search(topic)
    questions_json = json.loads(data_to_json(questions))
    return questions_json, gr.update(choices=[d["question"] for d in questions_json])


def create_first_page(data_state):
    # solution_visible = gr.State(False)
    with gr.Column(visible=True) as page1:
        gr.Markdown("# Programming in Python")
        with gr.Row():
            with gr.Column(scale=1):
                topic = gr.Textbox(label="Select Topic")
                submit2 = gr.Button("Submit", elem_id="submit2")

                with gr.Tab("Question"):
                    question_select = gr.Dropdown(
                        label="Select Question", choices=[], interactive=True
                    )
                    question_display = gr.Textbox(label="Question", interactive=False)

                with gr.Tab("Test Cases"):
                    testcases_state = gr.Markdown(label="Test Cases")

                with gr.Tab("Output"):
                    outputs_md = gr.Markdown(label="Output")  # JSON output component

                with gr.Tab("Solution 🔒"):
                    solution = gr.Code("Solution Locked")

            with gr.Column(scale=1):
                code_input = gr.Code(
                    label="Write your code here",
                    language="python",
                    lines=10,
                    interactive=True,
                )
                run_button = gr.Button("Run")

        # Connect buttons to functions
        submit2.click(
            fn=submit_second_page, inputs=[topic], outputs=[data_state, question_select]
        )

        question_select.change(
            fn=make_template_testcases,
            inputs=[question_select, data_state],
            outputs=[solution, question_display, testcases_state, code_input],
        )

        run_button.click(
            fn=lambda code, question, data: (
                make_template_outputs(code, question, data)
            ),
            inputs=[code_input, question_select, data_state],
            outputs=[outputs_md],
        )

    return page1, question_select


# Initialize the Gradio app
with gr.Blocks(css=".small-button { padding: 5px 10px; font-size: 12px; }") as demo:
    data_state = gr.State([])

    # First page is now the new topic and question selection interface
    page1_content, question_select = create_first_page(data_state)

demo.launch(share=True)