import gradio as gr def generate_code(language, task_description): # Шаблоны для генерации кода templates = { "Python": { "Hello World": "print('Hello, World!')", "Factorial": "def factorial(n):\n return 1 if n == 0 else n * factorial(n-1)", "Fibonacci": "def fibonacci(n):\n if n <= 1:\n return n\n else:\n return fibonacci(n-1) + fibonacci(n-2)", "Sum of two numbers": "def sum_two_numbers(a, b):\n return a + b", "Check if a number is prime": "def is_prime(n):\n if n <= 1:\n return False\n for i in range(2, int(n**0.5) + 1):\n if n % i == 0:\n return False\n return True", "Reverse a string": "def reverse_string(s):\n return s[::-1]", "Find the maximum number in a list": "def find_max(numbers):\n return max(numbers)", "Calculate the area of a circle": "import math\ndef circle_area(radius):\n return math.pi * radius ** 2", "Check if a string is a palindrome": "def is_palindrome(s):\n return s == s[::-1]", "Sort a list of numbers": "def sort_numbers(numbers):\n return sorted(numbers)", "Count the occurrences of a character in a string": "def count_char(s, char):\n return s.count(char)", "Generate a random number": "import random\ndef random_number():\n return random.randint(1, 100)", "Calculate the factorial using a loop": "def factorial_loop(n):\n result = 1\n for i in range(1, n + 1):\n result *= i\n return result", "Check if a year is a leap year": "def is_leap_year(year):\n return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)", "Find the GCD of two numbers": "import math\ndef gcd(a, b):\n return math.gcd(a, b)", "Convert Celsius to Fahrenheit": "def celsius_to_fahrenheit(celsius):\n return (celsius * 9/5) + 32", "Find the length of a list": "def list_length(lst):\n return len(lst)", "Check if a number is even": "def is_even(n):\n return n % 2 == 0", "Calculate the power of a number": "def power(base, exponent):\n return base ** exponent", "Find the minimum number in a list": "def find_min(numbers):\n return min(numbers)", "Calculate the sum of a list of numbers": "def sum_list(numbers):\n return sum(numbers)", "Check if a list contains a specific element": "def contains_element(lst, element):\n return element in lst", "Remove duplicates from a list": "def remove_duplicates(lst):\n return list(set(lst))" }, "JavaScript": { "Hello World": "console.log('Hello, World!');", "Factorial": "function factorial(n) {\n return n === 0 ? 1 : n * factorial(n - 1);\n}", "Fibonacci": "function fibonacci(n) {\n if (n <= 1) return n;\n return fibonacci(n - 1) + fibonacci(n - 2);\n}", "Sum of two numbers": "function sumTwoNumbers(a, b) {\n return a + b;\n}", "Check if a number is prime": "function isPrime(n) {\n if (n <= 1) return false;\n for (let i = 2; i <= Math.sqrt(n); i++) {\n if (n % i === 0) return false;\n }\n return true;\n}", "Reverse a string": "function reverseString(s) {\n return s.split('').reverse().join('');\n}", "Find the maximum number in a list": "function findMax(numbers) {\n return Math.max(...numbers);\n}", "Calculate the area of a circle": "function circleArea(radius) {\n return Math.PI * radius ** 2;\n}", "Check if a string is a palindrome": "function isPalindrome(s) {\n return s === s.split('').reverse().join('');\n}", "Sort a list of numbers": "function sortNumbers(numbers) {\n return numbers.sort((a, b) => a - b);\n}", "Count the occurrences of a character in a string": "function countChar(s, char) {\n return s.split(char).length - 1;\n}", "Generate a random number": "function randomNumber() {\n return Math.floor(Math.random() * 100) + 1;\n}", "Calculate the factorial using a loop": "function factorialLoop(n) {\n let result = 1;\n for (let i = 1; i <= n; i++) {\n result *= i;\n }\n return result;\n}", "Check if a year is a leap year": "function isLeapYear(year) {\n return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);\n}", "Find the GCD of two numbers": "function gcd(a, b) {\n return b === 0 ? a : gcd(b, a % b);\n}", "Convert Celsius to Fahrenheit": "function celsiusToFahrenheit(celsius) {\n return (celsius * 9/5) + 32;\n}", "Find the length of a list": "function listLength(lst) {\n return lst.length;\n}", "Check if a number is even": "function isEven(n) {\n return n % 2 === 0;\n}", "Calculate the power of a number": "function power(base, exponent) {\n return base ** exponent;\n}", "Find the minimum number in a list": "function findMin(numbers) {\n return Math.min(...numbers);\n}", "Calculate the sum of a list of numbers": "function sumList(numbers) {\n return numbers.reduce((a, b) => a + b, 0);\n}", "Check if a list contains a specific element": "function containsElement(lst, element) {\n return lst.includes(element);\n}", "Remove duplicates from a list": "function removeDuplicates(lst) {\n return [...new Set(lst)];\n}" }, "Java": { "Hello World": "public class Main {\n public static void main(String[] args) {\n System.out.println(\"Hello, World!\");\n }\n}", "Factorial": "public class Main {\n public static int factorial(int n) {\n return n == 0 ? 1 : n * factorial(n - 1);\n }\n}", "Fibonacci": "public class Main {\n public static int fibonacci(int n) {\n if (n <= 1) return n;\n return fibonacci(n - 1) + fibonacci(n - 2);\n }\n}", "Sum of two numbers": "public class Main {\n public static int sumTwoNumbers(int a, int b) {\n return a + b;\n }\n}", "Check if a number is prime": "public class Main {\n public static boolean isPrime(int n) {\n if (n <= 1) return false;\n for (int i = 2; i <= Math.sqrt(n); i++) {\n if (n % i == 0) return false;\n }\n return true;\n }\n}", "Reverse a string": "public class Main {\n public static String reverseString(String s) {\n return new StringBuilder(s).reverse().toString();\n }\n}", "Find the maximum number in a list": "import java.util.Collections;\nimport java.util.List;\n\npublic class Main {\n public static int findMax(List numbers) {\n return Collections.max(numbers);\n }\n}", "Calculate the area of a circle": "public class Main {\n public static double circleArea(double radius) {\n return Math.PI * radius * radius;\n }\n}", "Check if a string is a palindrome": "public class Main {\n public static boolean isPalindrome(String s) {\n return s.equals(new StringBuilder(s).reverse().toString());\n }\n}", "Sort a list of numbers": "import java.util.Collections;\nimport java.util.List;\n\npublic class Main {\n public static List sortNumbers(List numbers) {\n Collections.sort(numbers);\n return numbers;\n }\n}", "Count the occurrences of a character in a string": "public class Main {\n public static int countChar(String s, char c) {\n int count = 0;\n for (int i = 0; i < s.length(); i++) {\n if (s.charAt(i) == c) count++;\n }\n return count;\n }\n}", "Generate a random number": "import java.util.Random;\n\npublic class Main {\n public static int randomNumber() {\n return new Random().nextInt(100) + 1;\n }\n}", "Calculate the factorial using a loop": "public class Main {\n public static int factorialLoop(int n) {\n int result = 1;\n for (int i = 1; i <= n; i++) {\n result *= i;\n }\n return result;\n }\n}", "Check if a year is a leap year": "public class Main {\n public static boolean isLeapYear(int year) {\n return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);\n }\n}", "Find the GCD of two numbers": "public class Main {\n public static int gcd(int a, int b) {\n return b == 0 ? a : gcd(b, a % b);\n }\n}", "Convert Celsius to Fahrenheit": "public class Main {\n public static double celsiusToFahrenheit(double celsius) {\n return (celsius * 9/5) + 32;\n }\n}", "Find the length of a list": "import java.util.List;\n\npublic class Main {\n public static int listLength(List lst) {\n return lst.size();\n }\n}", "Check if a number is even": "public class Main {\n public static boolean isEven(int n) {\n return n % 2 == 0;\n }\n}", "Calculate the power of a number": "public class Main {\n public static double power(double base, double exponent) {\n return Math.pow(base, exponent);\n }\n}", "Find the minimum number in a list": "import java.util.Collections;\nimport java.util.List;\n\npublic class Main {\n public static int findMin(List numbers) {\n return Collections.min(numbers);\n }\n}", "Calculate the sum of a list of numbers": "import java.util.List;\n\npublic class Main {\n public static int sumList(List numbers) {\n return numbers.stream().mapToInt(Integer::intValue).sum();\n }\n}", "Check if a list contains a specific element": "import java.util.List;\n\npublic class Main {\n public static boolean containsElement(List lst, Object element) {\n return lst.contains(element);\n }\n}", "Remove duplicates from a list": "import java.util.ArrayList;\nimport java.util.List;\nimport java.util.Set;\nimport java.util.HashSet;\n\npublic class Main {\n public static List removeDuplicates(List lst) {\n return new ArrayList<>(new HashSet<>(lst));\n }\n}" } } # Поиск подходящего шаблона на основе описания задачи task_description = task_description.lower() for task, code in templates.get(language, {}).items(): if task.lower() in task_description: return code # Если задача не найдена return "Код для данной задачи не найден. Попробуйте описать задачу более четко." # Создаем интерфейс Gradio iface = gr.Interface( fn=generate_code, inputs=[ gr.Dropdown(choices=["Python", "JavaScript", "Java"], label="Выберите язык программирования"), gr.Textbox(label="Опишите задачу", placeholder="Например, 'напиши код для вычисления факториала'") ], outputs=gr.Code(label="Сгенерированный код"), title="Генератор кода", description="Выберите язык программирования и опишите задачу для генерации кода." ) # Запускаем интерфейс iface.launch()