Spaces:
Build error
Build error
Update app.py
Browse files8 unmodified lines9101112121314151617181920212223241516171822 unmodified lines414243534445464748495051521 unmodified line5455566162575859606165666263646566676869707172737475767778798081828384858687888990913 unmodified lines95969776779899100101102103104105106107108109110111112113114115801161171181193 unmodified lines1231241259012612712812913095131132971331341351368 unmodified linesmodel_name = "Qwen/Qwen2.5-72B-Instruct"client = InferenceClient(model_name)
def llm_inference(user_sample):def llm_inference(messages): 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}" }, ], messages=messages, stream=False, temperature=0.7, top_p=0.1,22 unmodified lines 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'] math_keywords = [ 'calculate', 'compute', 'solve', 'integrate', 'differentiate', 'derivative', 'integral', 'factorial', 'sum', 'product', 'average', 'mean', 'median', 'mode', 'variance', 'standard deviation', 'limit', 'matrix', 'determinant', 'equation', 'expression' ] 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)1 unmodified linedef 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. it performs a two-step process: 1. Generates an explanation of how to solve the task. 2. Generates Python code based on the explanation and executes it. """ 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}") # Step 1: Generate Explanation explanation_messages = [ { "role": "system", "content": "You are a Python language guide. Provide a concise explanation on how to approach the following mathematical task without calculating the answer." }, { "role": "user", "content": f"Provide a short explanation on how to solve the following mathematical problem: {user_input}" }, ] explanation = llm_inference(explanation_messages) # Step 2: Generate Python Code using Explanation and User Prompt 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}" code_messages = [ { "role": "system", "content": "You are a Python developer. Write Python code based on the provided explanation and task." }, { "role": "user", "content": f"{code_prompt}" }, ] generated_code = llm_inference(code_messages) # Strip code tags using regex # This regex removes ```python and ``` or any other markdown code fences3 unmodified lines # 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```" # Prepare the assistant response assistant_response = ( f"**Explanation:**\n{explanation}\n\n" f"**Generated Python Code:**\n```python\n{cleaned_code}\n```\n\n" f"**Execution Result:**\n```\n{execution_result}\n```" ) else: # For regular chat messages, use the AI's response messages = [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": user_input }, ] assistant_response = llm_inference(user_input) assistant_response = llm_inference(messages) # Append to chat history history.append((user_input, assistant_response))3 unmodified lines gr.Markdown("# 🐍 Python Helper Chatbot") with gr.Tab("Chat"): chatbot = gr.Chatbot() msg = gr.Textbox(placeholder="Type your message here...") msg = gr.Textbox(placeholder="Type your message here...", label="Your Message") msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot, chatbot]) with gr.Tab("Interpreter"): gr.Markdown("### 🖥️ Test Your Code") code_input = gr.Code(language="python") code_input = gr.Code(language="python", label="Python Code Input") run_button = gr.Button("Run Code") code_output = gr.Textbox(label="Output") code_output = gr.Textbox(label="Output", lines=10) run_button.click(execute_code, inputs=code_input, outputs=code_output) with gr.Tab("Logs"):app.py+58-22