diff --git a/week4/community-contributions/salah/unit-tests-generator/.env.example b/week4/community-contributions/salah/unit-tests-generator/.env.example new file mode 100644 index 0000000..da1d86d --- /dev/null +++ b/week4/community-contributions/salah/unit-tests-generator/.env.example @@ -0,0 +1 @@ +OPENROUTER_API_KEY=your-api-key-here diff --git a/week4/community-contributions/salah/unit-tests-generator/app.py b/week4/community-contributions/salah/unit-tests-generator/app.py new file mode 100644 index 0000000..f95ade0 --- /dev/null +++ b/week4/community-contributions/salah/unit-tests-generator/app.py @@ -0,0 +1,35 @@ +import gradio as gr +from test_generator import generate_tests + +def create_interface(): + with gr.Blocks(title="Unit Test Generator") as ui: + gr.Markdown("# Unit Test Generator") + gr.Markdown("Paste your Python code and get AI-generated unit tests") + + with gr.Row(): + with gr.Column(scale=1): + code_input = gr.Code( + label="Your Code", + language="python", + lines=15 + ) + generate_btn = gr.Button("Generate Tests", variant="primary") + + with gr.Column(scale=1): + tests_output = gr.Textbox( + label="Generated Tests", + lines=15, + interactive=False + ) + + generate_btn.click( + fn=generate_tests, + inputs=[code_input], + outputs=[tests_output] + ) + + return ui + +def launch(): + ui = create_interface() + ui.launch(server_name="localhost", server_port=7860) diff --git a/week4/community-contributions/salah/unit-tests-generator/main.py b/week4/community-contributions/salah/unit-tests-generator/main.py new file mode 100644 index 0000000..b43845c --- /dev/null +++ b/week4/community-contributions/salah/unit-tests-generator/main.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 + +import os +from dotenv import load_dotenv +from app import launch + +load_dotenv() + +if __name__ == "__main__": + api_key = os.getenv("OPENROUTER_API_KEY") + if not api_key: + print("Error: OPENROUTER_API_KEY not set in .env") + exit(1) + + print("Starting Unit Test Generator...") + print("Open http://localhost:7860 in your browser") + launch() diff --git a/week4/community-contributions/salah/unit-tests-generator/test_generator.py b/week4/community-contributions/salah/unit-tests-generator/test_generator.py new file mode 100644 index 0000000..402a239 --- /dev/null +++ b/week4/community-contributions/salah/unit-tests-generator/test_generator.py @@ -0,0 +1,41 @@ +import os +from openai import OpenAI +from dotenv import load_dotenv + +load_dotenv() + +client = OpenAI( + api_key=os.getenv("OPENROUTER_API_KEY"), + base_url="https://openrouter.ai/api/v1" +) + +MODEL = os.getenv("SECURECODE_MODEL", "meta-llama/llama-3.1-8b-instruct:free") + +SYSTEM_PROMPT = """You are a Python testing expert. +Generate pytest unit tests for the given code. +Include: +- Happy path tests +- Edge cases +- Error handling tests +Keep tests simple and clear.""" + +def generate_tests(code): + """Generate unit tests for the given code.""" + try: + response = client.chat.completions.create( + model=MODEL, + messages=[ + {"role": "system", "content": SYSTEM_PROMPT}, + {"role": "user", "content": f"Generate tests for this code:\n\n{code}"} + ], + stream=True + ) + + result = "" + for chunk in response: + if chunk.choices[0].delta.content: + result += chunk.choices[0].delta.content + yield result + + except Exception as e: + yield f"Error: {str(e)}"