Spaces:
Build error
Build error
Update app.py
Browse files2 unmodified lines34567893 unmodified lines131415151616171819202122232425266 unmodified lines33343529303132333637386 unmodified lines45464746474849505148495051525354555657585960616263646566676869707172737475767778798081538283848586879 unmodified lines979899697071727374757677781001011022 unmodified linesimport sysimport ioimport tracebackimport re # Import the regular expressions module
# Initialize the AI modelmodel_name = "Qwen/Qwen2.5-72B-Instruct"3 unmodified lines eos_token = "<|endoftext|>" output = client.chat.completions.create( messages=[ {"role": "system", "content": "You are a Python language guide. Write code on the user topic. If the input is code, correct it for mistakes."}, {"role": "user", "content": f"Write only python code without any explanation: {user_sample}"}, { "role": "system", "content": "You are a Python language guide. Write code on the user topic. If the input is code, correct it for mistakes." }, { "role": "user", "content": f"Write only python code without any explanation: {user_sample}" }, ], stream=False, temperature=0.7,6 unmodified lines response += choice['message']['content'] return response
def chat(user_input, history): response = llm_inference(user_input) history.append((user_input, response)) return history, history
def execute_code(code): old_stdout = sys.stdout redirected_output = sys.stdout = io.StringIO()6 unmodified lines sys.stdout = old_stdout return output
def solve_math_task(math_task): # Generate Python code for the math task generated_code = llm_inference(f"Create a Python program to solve the following math problem:\n{math_task}") # Execute the generated code execution_result = execute_code(generated_code)def is_math_task(user_input): """ Simple heuristic to determine if the user input is a math task. This can be enhanced with more sophisticated methods or NLP techniques. """ math_keywords = ['calculate', 'compute', 'solve', 'integrate', 'differentiate', 'derivative', 'integral', 'factorial', 'sum', 'product'] operators = ['+', '-', '*', '/', '^', '**', 'sqrt', 'sin', 'cos', 'tan', 'log', 'exp'] user_input_lower = user_input.lower() return any(keyword in user_input_lower for keyword in math_keywords) or any(op in user_input for op in operators)
def chat(user_input, history): """ Handles the chat interaction. If the user input is detected as a math task, it generates Python code to solve it, strips any code tags, executes the code, and returns the result. """ if is_math_task(user_input): # Generate Python code for the math task generated_code = llm_inference(f"Create a Python program to solve the following math problem:\n{user_input}") # Strip code tags using regex # This regex removes ```python and ``` or any other markdown code fences cleaned_code = re.sub(r"```(?:python)?\n?", "", generated_code).strip() cleaned_code = re.sub(r"```", "", cleaned_code).strip() # Execute the cleaned code execution_result = execute_code(cleaned_code) # Prepare the responses assistant_response = f"**Generated Python Code:**\n```python\n{cleaned_code}\n```\n\n**Execution Result:**\n```\n{execution_result}\n```" else: # For regular chat messages, use the AI's response assistant_response = llm_inference(user_input) return generated_code, execution_result # Append to chat history history.append((user_input, assistant_response)) return history, history
with gr.Blocks() as demo: gr.Markdown("# ๐ Python Helper Chatbot")9 unmodified lines code_output = gr.Textbox(label="Output") run_button.click(execute_code, inputs=code_input, outputs=code_output) with gr.Tab("Math Solver"): gr.Markdown("### ๐ Math Task Solver") math_input = gr.Textbox(placeholder="Enter your mathematical task here...", lines=2) solve_button = gr.Button("Solve Task") with gr.Row(): generated_code_output = gr.Code(label="Generated Python Code", language="python") with gr.Row(): execution_output = gr.Textbox(label="Execution Result", lines=10) solve_button.click(solve_math_task, inputs=math_input, outputs=[generated_code_output, execution_output]) with gr.Tab("Logs"): gr.Markdown("### ๐ Logs") log_output = gr.Textbox(label="Logs", lines=10, interactive=False)app.py+45-24