Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
| 6 |
|
| 7 |
def explain_code(
|
| 8 |
code_snippet,
|
| 9 |
-
history
|
| 10 |
system_message: str,
|
| 11 |
max_tokens: int,
|
| 12 |
temperature: float,
|
|
@@ -14,59 +14,56 @@ def explain_code(
|
|
| 14 |
):
|
| 15 |
# Prepare messages for the model
|
| 16 |
messages = [{"role": "system", "content": system_message}]
|
| 17 |
-
|
| 18 |
# Append previous interactions
|
| 19 |
for val in history:
|
| 20 |
-
if val[
|
| 21 |
-
messages.append({"role": "user", "content": val[
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "assistant", "content": val[
|
| 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 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
return response
|
| 42 |
|
| 43 |
-
# Example inputs
|
| 44 |
examples = [
|
| 45 |
-
["x = 5\nprint(x)"],
|
| 46 |
-
["def add(a, b):\n return a + b\nresult = add(2, 3)\nprint(result)"],
|
| 47 |
-
["for i in range(5):\n print(i)"],
|
| 48 |
-
["if x > 0:\n print('Positive')\nelse:\n print('Non-positive')"],
|
| 49 |
-
["squares = [i**2 for i in range(10)]\nprint(squares)"],
|
| 50 |
]
|
| 51 |
|
| 52 |
-
# Create the Gradio
|
| 53 |
-
demo = gr.
|
| 54 |
-
explain_code,
|
| 55 |
-
|
|
|
|
| 56 |
gr.Textbox(value="You are an expert assistant that explains Python code snippets clearly and concisely.", label="System message"),
|
| 57 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 58 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 59 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 60 |
],
|
| 61 |
examples=examples,
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# Update the code snippet input to be a multi-line textbox
|
| 65 |
-
demo.update_component(
|
| 66 |
-
"textbox",
|
| 67 |
-
gr.Textbox(placeholder="Enter your code snippet here...", label="Code Snippet", lines=10) # Multi-line input
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
demo.launch()
|
| 72 |
-
|
|
|
|
| 6 |
|
| 7 |
def explain_code(
|
| 8 |
code_snippet,
|
| 9 |
+
history,
|
| 10 |
system_message: str,
|
| 11 |
max_tokens: int,
|
| 12 |
temperature: float,
|
|
|
|
| 14 |
):
|
| 15 |
# Prepare messages for the model
|
| 16 |
messages = [{"role": "system", "content": system_message}]
|
| 17 |
+
|
| 18 |
# Append previous interactions
|
| 19 |
for val in history:
|
| 20 |
+
if val['role'] == 'user':
|
| 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.choices[0].delta.content
|
| 39 |
+
response += token
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"An error occurred: {str(e)}"
|
| 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
|
| 55 |
+
demo = gr.Interface(
|
| 56 |
+
fn=explain_code,
|
| 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(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 61 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 62 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 63 |
],
|
| 64 |
examples=examples,
|
| 65 |
+
title="Python Code Explainer"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|
|
|