|
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(): |
|
|
|
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) |
|
|
|
|
|
demo.css = """ |
|
#box { |
|
overflow-y: scroll; |
|
} |
|
""" |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |