Download Backend.py from valiant21/Python_Code_Assistance: direct link, hf CLI and curl.
- Browser
- Download file 1.89 kB
-
https://huggingface.co/spaces/valiant21/Python_Code_Assistance/resolve/21375d0e99e3921b37b4bc2609b5a1ccedfe9807/Backend.py
- Command line
-
hf download hf://spaces/valiant21/Python_Code_Assistance@21375d0e99e3921b37b4bc2609b5a1ccedfe9807/Backend.py
-
curl -L -o Backend.py https://huggingface.co/spaces/valiant21/Python_Code_Assistance/resolve/21375d0e99e3921b37b4bc2609b5a1ccedfe9807/Backend.py
1.89 kB
| import os | |
| import google.generativeai as generativeai | |
| from dotenv import load_dotenv | |
| # Load API key from environment | |
| load_dotenv() | |
| generativeai.configure(api_key=os.getenv("GOOGLE_GEMINI_KEY")) | |
| def get_correction_and_comments(code_snippet): | |
| """ | |
| Analyze, correct, and comment on the given Python code. | |
| """ | |
| prompt = [ | |
| "Analyze and correct the following Python code, add comments, and format it:", | |
| code_snippet | |
| ] | |
| response = generativeai.GenerativeModel('gemini-pro').generate_content(prompt) | |
| return response.text if response else "No suggestions available." | |
| def generate_questions(code_snippet, question_type): | |
| """ | |
| Generate questions and answers based on the user's choice of question type. | |
| Parameters: | |
| - code_snippet: The Python code to generate questions for. | |
| - question_type: The type of questions to generate. | |
| Returns: | |
| - Generated questions and answers as text. | |
| """ | |
| if question_type == "Logical Questions": | |
| prompt = [ | |
| "Analyze the following Python code and generate logical reasoning questions and answers:", | |
| code_snippet | |
| ] | |
| elif question_type == "Interview-Based Questions": | |
| prompt = [ | |
| "Analyze the following Python code and generate interview-style questions and answers for developers:", | |
| code_snippet | |
| ] | |
| elif question_type == "Code Analysis Questions": | |
| prompt = [ | |
| "Analyze the following Python code and generate in-depth code analysis questions with answers:", | |
| code_snippet | |
| ] | |
| else: | |
| prompt = [ | |
| "Generate general Python questions and answers based on the given code:", | |
| code_snippet | |
| ] | |
| response = generativeai.GenerativeModel('gemini-pro').generate_content(prompt) | |
| return response.text if response else "No answer available." | |