File size: 2,982 Bytes
40ef4a5
ca93b8e
40ef4a5
 
 
ca93b8e
 
40ef4a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca93b8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40ef4a5
ca93b8e
 
 
 
 
 
40ef4a5
 
 
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
import gradio as gr
import subprocess
from huggingface_hub import InferenceClient

client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
question = "Fibonacci series."
prompt = f"You are an expert in coding. your task is to explain error and give hint to understand question{question}.Do not give complete answer.Do not give implemmentation."
def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
):
    messages = [{"role": "system", "content": system_message}]

    for val in history:
        if val[0]:
            messages.append({"role": "user", "content": val[0]})
        if val[1]:
            messages.append({"role": "assistant", "content": val[1]})

    messages.append({"role": "user", "content": message})

    response = ""

    for message in client.chat_completion(
        messages,
        max_tokens=max_tokens,
        stream=True,
        temperature=temperature,
        top_p=top_p,
    ):
        token = message.choices[0].delta.content
        response += token
        yield response

def run_python_code(code):
    try:
        result = subprocess.run(['python3', '-c', code], capture_output=True, text=True)
        output = result.stdout if result.stdout else result.stderr
        return output
    except Exception as e:
        return str(e)

def AI_analyse(output):
    try:
        system_message = prompt
        max_tokens = 512
        temperature = 0.7
        top_p = 0.95
        message = prompt + "Please analyse the following code:\n" + output
        response = respond(message, [], system_message, max_tokens, temperature, top_p)
        for word in response:
            res=str(word)
        return res
    except Exception as e:
        return str(e)

with gr.Blocks() as demo:
    gr.Markdown("# Code Wiz")

    with gr.Row():
        with gr.Column():
            #question = gr.Markdown("### Question: Write a program to print Fibonacci series.")
            gr.Textbox(label="Question: Write a program to print Fibonacci series.", lines=1,interactive=False)
    with gr.Row():     
        with gr.Column():
            code = gr.Code(label="Python Code", language="python", lines=5,elem_id="box")
            run_button = gr.Button("Run")
    with gr.Row():
        with gr.Column():
            output = gr.Textbox(label="Output", lines=3, max_lines=20, interactive=False, elem_id="box")
           
    with gr.Row():
        with gr.Column():
            analyse_button = gr.Button("Analyse")
            ai_suggestion = gr.Textbox(label="AI Suggest", lines=7, placeholder="AI suggestions will be displayed here", interactive=False,elem_id="box")
            

    run_button.click(fn=run_python_code, inputs=code, outputs=output)
    analyse_button.click(fn=AI_analyse, inputs=output, outputs=ai_suggestion)

    # Add custom CSS
    demo.css = """
    #box {
        overflow-y: scroll; 
    }
    """

if __name__ == "__main__":
    demo.launch()