| import subprocess |
| import spaces |
| import os |
| import gradio as gr |
| import json |
| import base64 |
| from huggingface_hub import InferenceClient, login |
|
|
| os.system('apt-get update') |
|
|
| node = "source /home/user/.bashrc && source /home/user/.nvm/nvm.sh && nvm install lts/iron && npm update -g npm" |
| subprocess.call(["pip","install","--upgrade","pip"]) |
| subprocess.call("curl -o- file:///home/user/app/data/setup.sh | bash", shell=True, executable='/bin/bash') |
| subprocess.Popen(node, shell=True, executable='/bin/bash').wait() |
|
|
| app_n8n = "npm install -g n8n" |
| subprocess.Popen(app_n8n, shell=True, executable='/bin/bash').wait() |
|
|
| service_env = "npm install -g forever" |
| subprocess.Popen(service_env, shell=True, executable='/bin/bash').wait() |
| subprocess.call(["forever","start","/home/user/.nvm/versions/node/v20.15.1/bin/n8n","start","--tunnel"]) |
|
|
| |
| key = "UCODE_SECRET" |
| login(os.getenv(key)) |
|
|
| |
| client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct") |
|
|
| def decode_base64_to_json(base64_str): |
| try: |
| |
| decoded_bytes = base64.b64decode(base64_str) |
| |
| decoded_str = decoded_bytes.decode('utf-8') |
| |
| decoded_str = decoded_str.replace("\\'", "'").replace('\\"', '"').replace('\\\\', '\\') |
| print(f"===================================================\nDecoded string: {decoded_str}\n===================================================") |
| |
| return json.loads(decoded_str) |
| except Exception as e: |
| raise ValueError(f"Error decoding base64 to JSON: {str(e)}") |
|
|
|
|
| @spaces.GPU(enable_queue=True) |
| def chat_completion(user_input, max_tokens, temperature, top_p): |
| try: |
| |
| input_data = decode_base64_to_json(user_input) |
| |
| |
| if not isinstance(input_data, list): |
| raise ValueError("Input must be a list of messages.") |
| |
| response = "" |
| |
| |
| for message in client.chat_completion( |
| input_data, |
| max_tokens=max_tokens, |
| stream=True, |
| temperature=temperature, |
| top_p=top_p, |
| ): |
| token = message.choices[0].delta.get("content", "") |
| response += token |
| |
| return json.dumps({"status": "success", "output": response}) |
| except Exception as e: |
| return json.dumps({"status": "error", "message": str(e)}) |
|
|
| |
| user_input = gr.Textbox(label="User Input as Base64-encoded JSON String", lines=10) |
| max_tokens = gr.Slider(minimum=1, maximum=8092, value=150, label="Max Tokens") |
| temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.7, label="Temperature") |
| top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.9, label="Top P") |
|
|
| |
| iface = gr.Interface( |
| fn=chat_completion, |
| inputs=[user_input, max_tokens, temperature, top_p], |
| outputs="text", |
| title="UCode Agent", |
| description="Provide Base64-encoded JSON input with a list of messages and set the max tokens, temperature, and top_p to generate a chat completion." |
| ) |
|
|
| |
| iface.launch() |