Merge pull request #858 from msrashed2018/salah/bootcamp-week4
Salah/bootcamp week4
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
OPENROUTER_API_KEY=your-api-key-here
|
||||||
@@ -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)
|
||||||
@@ -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()
|
||||||
@@ -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)}"
|
||||||
Reference in New Issue
Block a user