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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Initialize clients for all models
|
| 6 |
+
models = {
|
| 7 |
+
"Exquisitely": "arthu1/Exquisitely",
|
| 8 |
+
"Olympus": "arthu1/Olympus",
|
| 9 |
+
"Astryx": "arthu1/Astryx",
|
| 10 |
+
"Aether": "arthu1/Aether"
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
clients = {name: InferenceClient(model) for name, model in models.items()}
|
| 14 |
+
|
| 15 |
+
# File system simulation
|
| 16 |
+
file_system = {}
|
| 17 |
+
|
| 18 |
+
def generate_response(message, model_name, history):
|
| 19 |
+
client = clients[model_name]
|
| 20 |
+
|
| 21 |
+
# Format conversation history
|
| 22 |
+
messages = []
|
| 23 |
+
for h in history:
|
| 24 |
+
messages.append({"role": "user", "content": h[0]})
|
| 25 |
+
messages.append({"role": "assistant", "content": h[1]})
|
| 26 |
+
messages.append({"role": "user", "content": message})
|
| 27 |
+
|
| 28 |
+
response = ""
|
| 29 |
+
for msg in client.chat_completion(
|
| 30 |
+
messages=messages,
|
| 31 |
+
max_tokens=2000,
|
| 32 |
+
stream=True,
|
| 33 |
+
):
|
| 34 |
+
token = msg.choices[0].delta.content
|
| 35 |
+
if token:
|
| 36 |
+
response += token
|
| 37 |
+
yield response
|
| 38 |
+
|
| 39 |
+
# Parse for files
|
| 40 |
+
if "```" in response:
|
| 41 |
+
import re
|
| 42 |
+
file_pattern = r"(?:FILENAME|filename|File):\s*(\S+)\s*```(?:\w+)?\n([\s\S]+?)```"
|
| 43 |
+
matches = re.findall(file_pattern, response)
|
| 44 |
+
for filename, content in matches:
|
| 45 |
+
file_system[filename] = content.strip()
|
| 46 |
+
|
| 47 |
+
def execute_code(code, language):
|
| 48 |
+
if language == "python":
|
| 49 |
+
try:
|
| 50 |
+
import io
|
| 51 |
+
import sys
|
| 52 |
+
from contextlib import redirect_stdout
|
| 53 |
+
|
| 54 |
+
output = io.StringIO()
|
| 55 |
+
with redirect_stdout(output):
|
| 56 |
+
exec(code)
|
| 57 |
+
return output.getvalue() or "✓ Executed successfully"
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error: {str(e)}"
|
| 60 |
+
elif language == "javascript":
|
| 61 |
+
return "JavaScript execution not available in this environment"
|
| 62 |
+
else:
|
| 63 |
+
return "Language not supported"
|
| 64 |
+
|
| 65 |
+
def list_files():
|
| 66 |
+
if not file_system:
|
| 67 |
+
return "No files created yet"
|
| 68 |
+
return "\n".join([f"📄 {name}" for name in file_system.keys()])
|
| 69 |
+
|
| 70 |
+
def read_file(filename):
|
| 71 |
+
return file_system.get(filename, f"File '{filename}' not found")
|
| 72 |
+
|
| 73 |
+
def download_file(filename):
|
| 74 |
+
content = file_system.get(filename, "")
|
| 75 |
+
if content:
|
| 76 |
+
return content
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
+
# Gradio Interface
|
| 80 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 81 |
+
gr.Markdown("# 💻 Algorix Coding IDE")
|
| 82 |
+
gr.Markdown("Test all Algorix models in an interactive environment")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column(scale=2):
|
| 86 |
+
model_selector = gr.Dropdown(
|
| 87 |
+
choices=list(models.keys()),
|
| 88 |
+
value="Aether",
|
| 89 |
+
label="Select Model"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
chatbot = gr.Chatbot(height=400, label="AI Assistant")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
msg = gr.Textbox(
|
| 96 |
+
label="Message",
|
| 97 |
+
placeholder="Ask me to write code...",
|
| 98 |
+
lines=3
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
submit = gr.Button("Send", variant="primary")
|
| 103 |
+
clear = gr.Button("Clear")
|
| 104 |
+
|
| 105 |
+
with gr.Column(scale=1):
|
| 106 |
+
gr.Markdown("### 🐧 Terminal")
|
| 107 |
+
|
| 108 |
+
terminal_output = gr.Textbox(
|
| 109 |
+
label="Output",
|
| 110 |
+
lines=10,
|
| 111 |
+
interactive=False,
|
| 112 |
+
value="Welcome to Algorix Terminal\n$"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
with gr.Row():
|
| 116 |
+
cmd_input = gr.Textbox(
|
| 117 |
+
label="Command",
|
| 118 |
+
placeholder="ls, cat <file>, python <file>"
|
| 119 |
+
)
|
| 120 |
+
run_cmd = gr.Button("Run")
|
| 121 |
+
|
| 122 |
+
gr.Markdown("### 📁 Files")
|
| 123 |
+
file_list = gr.Textbox(
|
| 124 |
+
label="Created Files",
|
| 125 |
+
lines=5,
|
| 126 |
+
interactive=False
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
with gr.Row():
|
| 130 |
+
file_name = gr.Textbox(label="Filename", scale=3)
|
| 131 |
+
download_btn = gr.Button("Download", scale=1)
|
| 132 |
+
|
| 133 |
+
file_content = gr.Textbox(label="File Content", lines=8)
|
| 134 |
+
|
| 135 |
+
def handle_command(cmd, current_output):
|
| 136 |
+
parts = cmd.strip().split()
|
| 137 |
+
if not parts:
|
| 138 |
+
return current_output
|
| 139 |
+
|
| 140 |
+
command = parts[0]
|
| 141 |
+
output = current_output + f"\n$ {cmd}\n"
|
| 142 |
+
|
| 143 |
+
if command == "ls":
|
| 144 |
+
output += "\n".join(file_system.keys()) or "outputs/"
|
| 145 |
+
elif command == "cat" and len(parts) > 1:
|
| 146 |
+
output += file_system.get(parts[1], f"cat: {parts[1]}: No such file")
|
| 147 |
+
elif command == "python" and len(parts) > 1:
|
| 148 |
+
code = file_system.get(parts[1], "")
|
| 149 |
+
if code:
|
| 150 |
+
output += execute_code(code, "python")
|
| 151 |
+
else:
|
| 152 |
+
output += f"python: can't open file '{parts[1]}'"
|
| 153 |
+
elif command == "clear":
|
| 154 |
+
return "Welcome to Algorix Terminal\n$"
|
| 155 |
+
elif command == "help":
|
| 156 |
+
output += "Commands: ls, cat <file>, python <file>, clear, help"
|
| 157 |
+
else:
|
| 158 |
+
output += f"{command}: command not found"
|
| 159 |
+
|
| 160 |
+
return output + "\n"
|
| 161 |
+
|
| 162 |
+
def user_message(message, history):
|
| 163 |
+
return "", history + [[message, None]]
|
| 164 |
+
|
| 165 |
+
def bot_response(history, model_name):
|
| 166 |
+
user_msg = history[-1][0]
|
| 167 |
+
|
| 168 |
+
# Generate response
|
| 169 |
+
full_response = ""
|
| 170 |
+
for response in generate_response(user_msg, model_name, history[:-1]):
|
| 171 |
+
full_response = response
|
| 172 |
+
history[-1][1] = response
|
| 173 |
+
yield history
|
| 174 |
+
|
| 175 |
+
# Update file list after generation
|
| 176 |
+
return history
|
| 177 |
+
|
| 178 |
+
# Event handlers
|
| 179 |
+
msg.submit(user_message, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 180 |
+
bot_response, [chatbot, model_selector], chatbot
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
submit.click(user_message, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 184 |
+
bot_response, [chatbot, model_selector], chatbot
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 188 |
+
|
| 189 |
+
run_cmd.click(
|
| 190 |
+
handle_command,
|
| 191 |
+
[cmd_input, terminal_output],
|
| 192 |
+
terminal_output
|
| 193 |
+
).then(lambda: "", None, cmd_input)
|
| 194 |
+
|
| 195 |
+
# Update file list every second
|
| 196 |
+
demo.load(list_files, None, file_list, every=1)
|
| 197 |
+
|
| 198 |
+
file_name.change(read_file, file_name, file_content)
|
| 199 |
+
|
| 200 |
+
download_btn.click(
|
| 201 |
+
download_file,
|
| 202 |
+
file_name,
|
| 203 |
+
gr.File(label="Download")
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
if __name__ == "__main__":
|
| 207 |
+
demo.launch()
|