import gradio as gr from huggingface_hub import hf_hub_download from llama_cpp import Llama import re import random import logging import os import jwt from typing import Dict, Any import autopep8 import textwrap import json from datasets import load_dataset from fastapi.responses import StreamingResponse import random # Set up logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Load the dataset (you might want to do this once at the start of your script) dataset = load_dataset("sugiv/leetmonkey_python_dataset") train_dataset = dataset["train"] # Set up logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # JWT settings JWT_SECRET = os.environ.get("JWT_SECRET") if not JWT_SECRET: raise ValueError("JWT_SECRET environment variable is not set") JWT_ALGORITHM = "HS256" # Model settings MODEL_NAME = "leetmonkey_peft__q8_0.gguf" REPO_ID = "sugiv/leetmonkey-peft-gguf" # Load the model model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_NAME, cache_dir="./models") llm = Llama(model_path=model_path, n_ctx=1024, n_threads=8, n_gpu_layers=-1, verbose=False, mlock=True) logger.info("8-bit model loaded successfully") # Generation parameters generation_kwargs = { "max_tokens": 512, "stop": ["```", "### Instruction:", "### Response:"], "echo": False, "temperature": 0.05, "top_k": 10, "top_p": 0.9, "repeat_penalty": 1.1 } def verify_token(token: str) -> bool: try: jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM]) return True except jwt.PyJWTError: return False def extract_and_format_code(text): # Extract code between triple backticks code_match = re.search(r'```python\s*(.*?)\s*```', text, re.DOTALL) if code_match: code = code_match.group(1) else: code = text # Dedent the code to remove any common leading whitespace code = textwrap.dedent(code) # Split the code into lines lines = code.split('\n') # Ensure proper indentation indented_lines = [] for line in lines: if line.strip().startswith('class') or line.strip().startswith('def'): indented_lines.append(line) # Keep class and function definitions as is elif line.strip(): # If the line is not empty indented_lines.append(' ' + line) # Add 4 spaces of indentation else: indented_lines.append(line) # Keep empty lines as is formatted_code = '\n'.join(indented_lines) try: return autopep8.fix_code(formatted_code) except: return formatted_code def generate_solution(instruction: str, token: str) -> Dict[str, Any]: if not verify_token(token): return {"error": "Invalid token"} system_prompt = "You are a Python coding assistant specialized in solving LeetCode problems. Provide only the complete implementation of the given function. Ensure proper indentation and formatting. Do not include any explanations or multiple solutions." full_prompt = f"""### Instruction: {system_prompt} Implement the following function for the LeetCode problem: {instruction} ### Response: Here's the complete Python function implementation: ```python """ generated_text = "" for chunk in llm(full_prompt, stream=True, **generation_kwargs): generated_text += chunk["choices"][0]["text"] formatted_code = extract_and_format_code(generated_text) return {"solution": formatted_code} def stream_solution(instruction: str, token: str): if not verify_token(token): return {"error": "Invalid token"} system_prompt = "You are a Python coding assistant specialized in solving LeetCode problems. Provide only the complete implementation of the given function. Ensure proper indentation and formatting. Do not include any explanations or multiple solutions." full_prompt = f"""### Instruction: {system_prompt} Implement the following function for the LeetCode problem: {instruction} ### Response: Here's the complete Python function implementation: ```python """ def generate(): generated_text = "" for chunk in llm(full_prompt, stream=True, **generation_kwargs): token = chunk["choices"][0]["text"] generated_text += token yield json.dumps({"token": token, "generated_text": generated_text}) + "\n" formatted_code = extract_and_format_code(generated_text) yield json.dumps({"complete": True, "formatted_code": formatted_code}) + "\n" generated_response = StreamingResponse(generate(), media_type="application/x-ndjson") logger.info(f"Streaming response {generated_response}") return StreamingResponse(generate(), media_type="application/text") def random_problem(token: str) -> Dict[str, Any]: if not verify_token(token): return {"error": "Invalid token"} # Select a random problem from the dataset random_item = random.choice(train_dataset) # Extract the instruction (problem statement) from the randomly selected item problem = random_item['instruction'] return {"problem": problem} # Create Gradio interfaces for each endpoint generate_interface = gr.Interface( fn=generate_solution, inputs=[gr.Textbox(label="Problem Instruction"), gr.Textbox(label="JWT Token")], outputs=gr.JSON(), title="Generate Solution API", description="Provide a LeetCode problem instruction and a valid JWT token to generate a solution." ) stream_interface = gr.Interface( fn=stream_solution, inputs=[gr.Textbox(label="Problem Instruction"), gr.Textbox(label="JWT Token")], outputs=gr.JSON(), title="Stream Solution API", description="Provide a LeetCode problem instruction and a valid JWT token to stream a solution." ) random_problem_interface = gr.Interface( fn=random_problem, inputs=gr.Textbox(label="JWT Token"), outputs=gr.JSON(), title="Random Problem API", description="Provide a valid JWT token to get a random LeetCode problem." ) # Combine interfaces demo = gr.TabbedInterface( [generate_interface, stream_interface, random_problem_interface], ["Generate Solution", "Stream Solution", "Random Problem"] ) if __name__ == "__main__": demo.launch(share=True)