Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
def explain_and_run_code(code_snippet):
|
|
|
|
| 8 |
system_message = "You are an expert assistant that explains Python code snippets clearly and concisely."
|
| 9 |
explanation = ""
|
| 10 |
output = ""
|
| 11 |
|
|
|
|
| 12 |
try:
|
| 13 |
messages = [{"role": "system", "content": system_message}]
|
| 14 |
messages.append({
|
| 15 |
"role": "user",
|
| 16 |
-
"content": f"Please provide a detailed explanation of the following code snippet:\n
|
|
|
|
|
|
|
| 17 |
})
|
| 18 |
|
| 19 |
for msg in client.chat_completion(
|
|
@@ -23,14 +28,16 @@ def explain_and_run_code(code_snippet):
|
|
| 23 |
temperature=0.7,
|
| 24 |
top_p=0.95
|
| 25 |
):
|
| 26 |
-
token = msg["choices"][0]["delta"]
|
| 27 |
explanation += token
|
| 28 |
except Exception as e:
|
| 29 |
explanation = f"An error occurred during explanation: {str(e)}"
|
| 30 |
|
|
|
|
| 31 |
try:
|
| 32 |
import io
|
| 33 |
import contextlib
|
|
|
|
| 34 |
output_buffer = io.StringIO()
|
| 35 |
with contextlib.redirect_stdout(output_buffer):
|
| 36 |
exec(code_snippet)
|
|
@@ -40,13 +47,12 @@ def explain_and_run_code(code_snippet):
|
|
| 40 |
|
| 41 |
return f"**Explanation:**\n{explanation}\n\n**Output:**\n{output}"
|
| 42 |
|
| 43 |
-
|
| 44 |
-
# Gradio Interface
|
| 45 |
demo = gr.Interface(
|
| 46 |
fn=explain_and_run_code,
|
| 47 |
inputs=gr.Textbox(placeholder="Enter your code snippet here...", label="Code Snippet", lines=10),
|
| 48 |
outputs=gr.Textbox(label="Explanation and Output", lines=15),
|
| 49 |
-
title="DECIPHER The Python Code Explainer\n\n
|
| 50 |
theme="default"
|
| 51 |
)
|
| 52 |
|
|
|
|
| 1 |
+
You said:
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Initialize the Hugging Face Inference Client
|
| 6 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta",token=hf_api_token)
|
| 7 |
|
| 8 |
def explain_and_run_code(code_snippet):
|
| 9 |
+
# Prepare a basic system message
|
| 10 |
system_message = "You are an expert assistant that explains Python code snippets clearly and concisely."
|
| 11 |
explanation = ""
|
| 12 |
output = ""
|
| 13 |
|
| 14 |
+
# Generate the explanation using the model
|
| 15 |
try:
|
| 16 |
messages = [{"role": "system", "content": system_message}]
|
| 17 |
messages.append({
|
| 18 |
"role": "user",
|
| 19 |
+
"content": f"Please provide a detailed explanation of the following code snippet:\n
|
| 20 |
+
{code_snippet}
|
| 21 |
+
"
|
| 22 |
})
|
| 23 |
|
| 24 |
for msg in client.chat_completion(
|
|
|
|
| 28 |
temperature=0.7,
|
| 29 |
top_p=0.95
|
| 30 |
):
|
| 31 |
+
token = msg["choices"][0]["delta"]["content"]
|
| 32 |
explanation += token
|
| 33 |
except Exception as e:
|
| 34 |
explanation = f"An error occurred during explanation: {str(e)}"
|
| 35 |
|
| 36 |
+
# Execute the code snippet and capture the output
|
| 37 |
try:
|
| 38 |
import io
|
| 39 |
import contextlib
|
| 40 |
+
|
| 41 |
output_buffer = io.StringIO()
|
| 42 |
with contextlib.redirect_stdout(output_buffer):
|
| 43 |
exec(code_snippet)
|
|
|
|
| 47 |
|
| 48 |
return f"**Explanation:**\n{explanation}\n\n**Output:**\n{output}"
|
| 49 |
|
| 50 |
+
# Create the Gradio Interface with centered title
|
|
|
|
| 51 |
demo = gr.Interface(
|
| 52 |
fn=explain_and_run_code,
|
| 53 |
inputs=gr.Textbox(placeholder="Enter your code snippet here...", label="Code Snippet", lines=10),
|
| 54 |
outputs=gr.Textbox(label="Explanation and Output", lines=15),
|
| 55 |
+
title="DECIPHER The Python Code Explainer\n\n Al Capstone Project\n (XII-C)",
|
| 56 |
theme="default"
|
| 57 |
)
|
| 58 |
|