Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,19 @@
|
|
1 |
-
You said:
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
5 |
-
#
|
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(
|
@@ -33,11 +28,9 @@ def explain_and_run_code(code_snippet):
|
|
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,12 +40,12 @@ def explain_and_run_code(code_snippet):
|
|
47 |
|
48 |
return f"**Explanation:**\n{explanation}\n\n**Output:**\n{output}"
|
49 |
|
50 |
-
|
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
|
56 |
theme="default"
|
57 |
)
|
58 |
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
# ✅ Add your Hugging Face API token here
|
5 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token="hf_api_token")
|
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```{code_snippet}```"
|
|
|
|
|
17 |
})
|
18 |
|
19 |
for msg in client.chat_completion(
|
|
|
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 |
|
41 |
return f"**Explanation:**\n{explanation}\n\n**Output:**\n{output}"
|
42 |
|
43 |
+
|
44 |
demo = gr.Interface(
|
45 |
fn=explain_and_run_code,
|
46 |
inputs=gr.Textbox(placeholder="Enter your code snippet here...", label="Code Snippet", lines=10),
|
47 |
outputs=gr.Textbox(label="Explanation and Output", lines=15),
|
48 |
+
title="DECIPHER The Python Code Explainer\n\n AI Capstone Project\n (XII-C)",
|
49 |
theme="default"
|
50 |
)
|
51 |
|