-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_generation.py
More file actions
90 lines (75 loc) · 3.28 KB
/
Copy pathcode_generation.py
File metadata and controls
90 lines (75 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Imports
from openai import OpenAI
import gradio as gr
from dotenv import load_dotenv
load_dotenv()
openrouter_url = "https://openrouter.ai/api/v1"
openrouter = OpenAI(api_key=os.getenv("OPENROUTER_API_KEY"), base_url=openrouter_url)
models = [
"GPT 5.1 Codex Mini",
"Mercury Coder",
"GPT 4.1 Nano",
"Mercury 2",
"Gemini 2.5 Flashlite"
]
clients = {
"openai/gpt-5.1-codex-mini": openrouter,
"inception/mercury-coder": openrouter,
"openai/gpt-4.1-nano": openrouter,
"inception/mercury-2": openrouter,
"google/gemini-2.5-flash-lite": openrouter
}
# Code system prompt - decides the tone and response of the model
system_prompt = """
You are a helpful coding assistant who generates very efficient code in the language they want.
The only objectives are to make it as fast as possible using ANY methods possible, make it have no errors, and to give the generated output.
Do not provide any explanation at all, assuming the user knows how to code. Only respond with the code.
"""
# Creates user prompt
def user_prompt_for(code_type, prompt):
return f"""
Generate {code_type} code with the fastest possible implementation in the least time.
Respond only with {code_type} code. Do NOT provide ANY explanation.
Task:
{prompt}
"""
# Creates the format that OpenAI recieves
def messages_for(prompt, code_type):
return [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt_for(code_type, prompt)}
]
# Runs the main model
def port(model, prompt, code_type):
client = clients[model.lower()]
stream = client.chat.completions.create(model = model, messages=messages_for(prompt, code_type), stream = True)
response = ""
for chunk in stream:
response += chunk.choices[0].delta.content or ''
yield response
coding_language = [
"🌐HTML: Front-end, simple, website-used programming",
"🎨CSS: Front and back-end stylist programming",
"🐍Python: Simple, object oriented programming",
"☕Java: High-level, object-oriented programming",
"➕➕C++: High-performance, compiled programming",
"#️⃣C#: Modern, general-purpose, object-oriented programming",
"🐘PHP: Widely-used, open-source, server-side scripting programming",
"🦀Rust: Modern, high-performance systems programming"
]
# Setting the theme using gradio
theme = gr.themes.Default(font=[gr.themes.GoogleFont("Source Code Pro")], primary_hue=gr.themes.colors.teal, secondary_hue=gr.themes.colors.sky).set(loader_color ="#FF0000")
# UI using gradio
with gr.Blocks(theme=theme, title="CODER-AI") as ui:
with gr.Row():
model = gr.Dropdown(models, label="Select Model", value=models[0])
code_type = gr.Dropdown(coding_language, label="Select Coding Language", value=coding_language[0])
with gr.Row():
prompt = gr.Textbox(label="Prompt the code you want our AI assistant to generate!", lines = 14, value = "Generate a code snippet calculating the logarithm of any number.")
code = gr.Code(label="Code", interactive=True, lines=14)
with gr.Row():
generate = gr.Button("Generate Code", variant="primary")
generate.click(port, inputs = [model, prompt, code_type], outputs=[code])
# Launches the User Interface
if __name__ == "__main__":
ui.launch(inbrowser=True, share=True)