Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,49 +6,42 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
| 6 |
|
| 7 |
def explain_code(
|
| 8 |
code_snippet,
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
top_p: float,
|
| 14 |
):
|
| 15 |
# Prepare messages for the model
|
| 16 |
messages = [{"role": "system", "content": system_message}]
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
messages.append({"role": "user", "content": val['content']})
|
| 22 |
-
elif val['role'] == 'assistant':
|
| 23 |
-
messages.append({"role": "assistant", "content": val['content']})
|
| 24 |
-
|
| 25 |
-
# Add the code snippet for explanation
|
| 26 |
-
messages.append({"role": "user", "content": f"Please provide a detailed explanation of the following code snippet:\n```{code_snippet}```"})
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
try:
|
|
|
|
| 31 |
for msg in client.chat_completion(
|
| 32 |
messages,
|
| 33 |
max_tokens=max_tokens,
|
| 34 |
stream=True,
|
| 35 |
temperature=temperature,
|
| 36 |
-
top_p=top_p
|
| 37 |
):
|
| 38 |
-
token = msg
|
| 39 |
response += token
|
| 40 |
except Exception as e:
|
| 41 |
-
|
| 42 |
|
| 43 |
return response
|
| 44 |
|
| 45 |
# Example inputs
|
| 46 |
examples = [
|
| 47 |
-
["x = 5\nprint(x)"],
|
| 48 |
["def add(a, b):\n return a + b\nresult = add(2, 3)\nprint(result)"],
|
| 49 |
-
["for i in range(5):\n print(i)"],
|
| 50 |
["if x > 0:\n print('Positive')\nelse:\n print('Non-positive')"],
|
| 51 |
-
["squares = [i**2 for i in range(10)]\nprint(squares)"]
|
| 52 |
]
|
| 53 |
|
| 54 |
# Create the Gradio Interface
|
|
@@ -57,11 +50,11 @@ demo = gr.Interface(
|
|
| 57 |
inputs=[
|
| 58 |
gr.Textbox(placeholder="Enter your code snippet here...", label="Code Snippet", lines=10),
|
| 59 |
gr.Textbox(value="You are an expert assistant that explains Python code snippets clearly and concisely.", label="System message"),
|
| 60 |
-
gr.Slider(
|
| 61 |
-
gr.Slider(
|
| 62 |
-
gr.Slider(
|
| 63 |
],
|
| 64 |
-
outputs=gr.Textbox(label="Explanation Here", lines=10),
|
| 65 |
examples=examples,
|
| 66 |
title="Python Code Explainer"
|
| 67 |
)
|
|
|
|
| 6 |
|
| 7 |
def explain_code(
|
| 8 |
code_snippet,
|
| 9 |
+
system_message,
|
| 10 |
+
max_tokens,
|
| 11 |
+
temperature,
|
| 12 |
+
top_p
|
|
|
|
| 13 |
):
|
| 14 |
# Prepare messages for the model
|
| 15 |
messages = [{"role": "system", "content": system_message}]
|
| 16 |
+
messages.append({
|
| 17 |
+
"role": "user",
|
| 18 |
+
"content": f"Please provide a detailed explanation of the following code snippet:\n```{code_snippet}```"
|
| 19 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
response = ""
|
|
|
|
| 22 |
try:
|
| 23 |
+
# Generate the response using the model
|
| 24 |
for msg in client.chat_completion(
|
| 25 |
messages,
|
| 26 |
max_tokens=max_tokens,
|
| 27 |
stream=True,
|
| 28 |
temperature=temperature,
|
| 29 |
+
top_p=top_p
|
| 30 |
):
|
| 31 |
+
token = msg["choices"][0]["delta"]["content"]
|
| 32 |
response += token
|
| 33 |
except Exception as e:
|
| 34 |
+
response = f"An error occurred: {str(e)}"
|
| 35 |
|
| 36 |
return response
|
| 37 |
|
| 38 |
# Example inputs
|
| 39 |
examples = [
|
| 40 |
+
["x = 5\nprint(x)"],
|
| 41 |
["def add(a, b):\n return a + b\nresult = add(2, 3)\nprint(result)"],
|
| 42 |
+
["for i in range(5):\n print(i)"],
|
| 43 |
["if x > 0:\n print('Positive')\nelse:\n print('Non-positive')"],
|
| 44 |
+
["squares = [i**2 for i in range(10)]\nprint(squares)"]
|
| 45 |
]
|
| 46 |
|
| 47 |
# Create the Gradio Interface
|
|
|
|
| 50 |
inputs=[
|
| 51 |
gr.Textbox(placeholder="Enter your code snippet here...", label="Code Snippet", lines=10),
|
| 52 |
gr.Textbox(value="You are an expert assistant that explains Python code snippets clearly and concisely.", label="System message"),
|
| 53 |
+
gr.Slider(1, 2048, 512, step=1, label="Max new tokens"),
|
| 54 |
+
gr.Slider(0.1, 4.0, 0.7, step=0.1, label="Temperature"),
|
| 55 |
+
gr.Slider(0.1, 1.0, 0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 56 |
],
|
| 57 |
+
outputs=gr.Textbox(label="Explanation Here", lines=10),
|
| 58 |
examples=examples,
|
| 59 |
title="Python Code Explainer"
|
| 60 |
)
|