Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,19 +9,10 @@ import re # Import the regular expressions module
|
|
| 9 |
model_name = "Qwen/Qwen2.5-72B-Instruct"
|
| 10 |
client = InferenceClient(model_name)
|
| 11 |
|
| 12 |
-
def llm_inference(
|
| 13 |
eos_token = "<|endoftext|>"
|
| 14 |
output = client.chat.completions.create(
|
| 15 |
-
messages=
|
| 16 |
-
{
|
| 17 |
-
"role": "system",
|
| 18 |
-
"content": "You are a Python language guide. Write code on the user topic. If the input is code, correct it for mistakes."
|
| 19 |
-
},
|
| 20 |
-
{
|
| 21 |
-
"role": "user",
|
| 22 |
-
"content": f"Write only python code without any explanation: {user_sample}"
|
| 23 |
-
},
|
| 24 |
-
],
|
| 25 |
stream=False,
|
| 26 |
temperature=0.7,
|
| 27 |
top_p=0.1,
|
|
@@ -50,7 +41,12 @@ def is_math_task(user_input):
|
|
| 50 |
Simple heuristic to determine if the user input is a math task.
|
| 51 |
This can be enhanced with more sophisticated methods or NLP techniques.
|
| 52 |
"""
|
| 53 |
-
math_keywords = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
operators = ['+', '-', '*', '/', '^', '**', 'sqrt', 'sin', 'cos', 'tan', 'log', 'exp']
|
| 55 |
user_input_lower = user_input.lower()
|
| 56 |
return any(keyword in user_input_lower for keyword in math_keywords) or any(op in user_input for op in operators)
|
|
@@ -58,12 +54,38 @@ def is_math_task(user_input):
|
|
| 58 |
def chat(user_input, history):
|
| 59 |
"""
|
| 60 |
Handles the chat interaction. If the user input is detected as a math task,
|
| 61 |
-
it
|
| 62 |
-
|
|
|
|
| 63 |
"""
|
| 64 |
if is_math_task(user_input):
|
| 65 |
-
#
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Strip code tags using regex
|
| 69 |
# This regex removes ```python and ``` or any other markdown code fences
|
|
@@ -73,11 +95,25 @@ def chat(user_input, history):
|
|
| 73 |
# Execute the cleaned code
|
| 74 |
execution_result = execute_code(cleaned_code)
|
| 75 |
|
| 76 |
-
# Prepare the
|
| 77 |
-
assistant_response =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
else:
|
| 79 |
# For regular chat messages, use the AI's response
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Append to chat history
|
| 83 |
history.append((user_input, assistant_response))
|
|
@@ -87,14 +123,14 @@ with gr.Blocks() as demo:
|
|
| 87 |
gr.Markdown("# 🐍 Python Helper Chatbot")
|
| 88 |
with gr.Tab("Chat"):
|
| 89 |
chatbot = gr.Chatbot()
|
| 90 |
-
msg = gr.Textbox(placeholder="Type your message here...")
|
| 91 |
msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
|
| 92 |
|
| 93 |
with gr.Tab("Interpreter"):
|
| 94 |
gr.Markdown("### 🖥️ Test Your Code")
|
| 95 |
-
code_input = gr.Code(language="python")
|
| 96 |
run_button = gr.Button("Run Code")
|
| 97 |
-
code_output = gr.Textbox(label="Output")
|
| 98 |
run_button.click(execute_code, inputs=code_input, outputs=code_output)
|
| 99 |
|
| 100 |
with gr.Tab("Logs"):
|
|
|
|
| 9 |
model_name = "Qwen/Qwen2.5-72B-Instruct"
|
| 10 |
client = InferenceClient(model_name)
|
| 11 |
|
| 12 |
+
def llm_inference(messages):
|
| 13 |
eos_token = "<|endoftext|>"
|
| 14 |
output = client.chat.completions.create(
|
| 15 |
+
messages=messages,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
stream=False,
|
| 17 |
temperature=0.7,
|
| 18 |
top_p=0.1,
|
|
|
|
| 41 |
Simple heuristic to determine if the user input is a math task.
|
| 42 |
This can be enhanced with more sophisticated methods or NLP techniques.
|
| 43 |
"""
|
| 44 |
+
math_keywords = [
|
| 45 |
+
'calculate', 'compute', 'solve', 'integrate', 'differentiate',
|
| 46 |
+
'derivative', 'integral', 'factorial', 'sum', 'product',
|
| 47 |
+
'average', 'mean', 'median', 'mode', 'variance', 'standard deviation',
|
| 48 |
+
'limit', 'matrix', 'determinant', 'equation', 'expression'
|
| 49 |
+
]
|
| 50 |
operators = ['+', '-', '*', '/', '^', '**', 'sqrt', 'sin', 'cos', 'tan', 'log', 'exp']
|
| 51 |
user_input_lower = user_input.lower()
|
| 52 |
return any(keyword in user_input_lower for keyword in math_keywords) or any(op in user_input for op in operators)
|
|
|
|
| 54 |
def chat(user_input, history):
|
| 55 |
"""
|
| 56 |
Handles the chat interaction. If the user input is detected as a math task,
|
| 57 |
+
it performs a two-step process:
|
| 58 |
+
1. Generates an explanation of how to solve the task.
|
| 59 |
+
2. Generates Python code based on the explanation and executes it.
|
| 60 |
"""
|
| 61 |
if is_math_task(user_input):
|
| 62 |
+
# Step 1: Generate Explanation
|
| 63 |
+
explanation_messages = [
|
| 64 |
+
{
|
| 65 |
+
"role": "system",
|
| 66 |
+
"content": "You are a Python language guide. Provide a concise explanation on how to approach the following mathematical task without calculating the answer."
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"role": "user",
|
| 70 |
+
"content": f"Provide a short explanation on how to solve the following mathematical problem: {user_input}"
|
| 71 |
+
},
|
| 72 |
+
]
|
| 73 |
+
explanation = llm_inference(explanation_messages)
|
| 74 |
+
|
| 75 |
+
# Step 2: Generate Python Code using Explanation and User Prompt
|
| 76 |
+
code_prompt = f"Based on the following explanation, write a Python program to solve the mathematical task. Ensure that the program includes a print statement to output the answer.\n\nExplanation: {explanation}\n\nTask: {user_input}"
|
| 77 |
+
|
| 78 |
+
code_messages = [
|
| 79 |
+
{
|
| 80 |
+
"role": "system",
|
| 81 |
+
"content": "You are a Python developer. Write Python code based on the provided explanation and task."
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"role": "user",
|
| 85 |
+
"content": f"{code_prompt}"
|
| 86 |
+
},
|
| 87 |
+
]
|
| 88 |
+
generated_code = llm_inference(code_messages)
|
| 89 |
|
| 90 |
# Strip code tags using regex
|
| 91 |
# This regex removes ```python and ``` or any other markdown code fences
|
|
|
|
| 95 |
# Execute the cleaned code
|
| 96 |
execution_result = execute_code(cleaned_code)
|
| 97 |
|
| 98 |
+
# Prepare the assistant response
|
| 99 |
+
assistant_response = (
|
| 100 |
+
f"**Explanation:**\n{explanation}\n\n"
|
| 101 |
+
f"**Generated Python Code:**\n```python\n{cleaned_code}\n```\n\n"
|
| 102 |
+
f"**Execution Result:**\n```\n{execution_result}\n```"
|
| 103 |
+
)
|
| 104 |
else:
|
| 105 |
# For regular chat messages, use the AI's response
|
| 106 |
+
messages = [
|
| 107 |
+
{
|
| 108 |
+
"role": "system",
|
| 109 |
+
"content": "You are a helpful assistant."
|
| 110 |
+
},
|
| 111 |
+
{
|
| 112 |
+
"role": "user",
|
| 113 |
+
"content": user_input
|
| 114 |
+
},
|
| 115 |
+
]
|
| 116 |
+
assistant_response = llm_inference(messages)
|
| 117 |
|
| 118 |
# Append to chat history
|
| 119 |
history.append((user_input, assistant_response))
|
|
|
|
| 123 |
gr.Markdown("# 🐍 Python Helper Chatbot")
|
| 124 |
with gr.Tab("Chat"):
|
| 125 |
chatbot = gr.Chatbot()
|
| 126 |
+
msg = gr.Textbox(placeholder="Type your message here...", label="Your Message")
|
| 127 |
msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
|
| 128 |
|
| 129 |
with gr.Tab("Interpreter"):
|
| 130 |
gr.Markdown("### 🖥️ Test Your Code")
|
| 131 |
+
code_input = gr.Code(language="python", label="Python Code Input")
|
| 132 |
run_button = gr.Button("Run Code")
|
| 133 |
+
code_output = gr.Textbox(label="Output", lines=10)
|
| 134 |
run_button.click(execute_code, inputs=code_input, outputs=code_output)
|
| 135 |
|
| 136 |
with gr.Tab("Logs"):
|