abenkbp commited on
Commit
098fa15
·
1 Parent(s): 88883ee
Files changed (1) hide show
  1. app.py +12 -28
app.py CHANGED
@@ -1,61 +1,48 @@
1
  import subprocess
2
- import spaces
3
  import os
4
  import gradio as gr
5
  import json
6
  import base64
7
  from huggingface_hub import InferenceClient, login
8
 
9
- reinstall = "APP_REINSTALL"
10
- if os.getenv(reinstall):
11
- node = "source /home/user/.bashrc && source /home/user/.nvm/nvm.sh && nvm install lts/iron && npm update -g npm"
12
- subprocess.call(["pip","install","--upgrade","pip"])
13
  subprocess.call("curl -o- file:///home/user/app/data/setup.sh | bash", shell=True, executable='/bin/bash')
14
- subprocess.Popen(node, shell=True, executable='/bin/bash').wait()
15
- app_n8n = "npm install -g n8n"
16
- subprocess.Popen(app_n8n, shell=True, executable='/bin/bash').wait()
17
- service_env = "npm install -g forever"
18
- subprocess.Popen(service_env, shell=True, executable='/bin/bash').wait()
19
  subprocess.call("curl -o- file:///home/user/app/data/config_nginx.sh | bash", shell=True, executable='/bin/bash')
20
 
21
- subprocess.call(["forever","start","/home/user/.nvm/versions/node/v20.16.0/bin/n8n"])
22
- subprocess.call(["forever","start","/usr/sbin/nginx"], shell=True, executable='/bin/bash')
23
 
24
  # Get the API key from environment variables
25
- key = "UCODE_SECRET"
26
- login(os.getenv(key))
27
 
28
  # Initialize the InferenceClient with the specified model
29
  client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct")
30
 
31
  def decode_base64_to_json(base64_str):
32
  try:
33
- # Decode the base64 string
34
  decoded_bytes = base64.b64decode(base64_str)
35
- # Convert bytes to string
36
  decoded_str = decoded_bytes.decode('utf-8')
37
- # Fix escaped characters
38
  decoded_str = decoded_str.replace("\\'", "'").replace('\\"', '"').replace('\\\\', '\\')
39
  print(f"===================================================\nDecoded string: {decoded_str}\n===================================================") # Log the decoded string
40
- # Parse the JSON string
41
  return json.loads(decoded_str)
42
  except Exception as e:
43
  raise ValueError(f"Error decoding base64 to JSON: {str(e)}")
44
 
45
-
46
  @spaces.GPU(enable_queue=True)
47
  def chat_completion(user_input, max_tokens, temperature, top_p):
48
  try:
49
- # Decode the base64-encoded JSON input
50
  input_data = decode_base64_to_json(user_input)
51
-
52
- # Ensure the input is a list of messages
53
  if not isinstance(input_data, list):
54
  raise ValueError("Input must be a list of messages.")
55
 
56
  response = ""
57
-
58
- # Generate chat completion
59
  for message in client.chat_completion(
60
  input_data,
61
  max_tokens=max_tokens,
@@ -70,13 +57,11 @@ def chat_completion(user_input, max_tokens, temperature, top_p):
70
  except Exception as e:
71
  return json.dumps({"status": "error", "message": str(e)})
72
 
73
- # Create Gradio components for user input
74
  user_input = gr.Textbox(label="User Input as Base64-encoded JSON String", lines=10)
75
  max_tokens = gr.Slider(minimum=1, maximum=8092, value=150, label="Max Tokens")
76
  temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.7, label="Temperature")
77
  top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.9, label="Top P")
78
 
79
- # Set up Gradio interface
80
  iface = gr.Interface(
81
  fn=chat_completion,
82
  inputs=[user_input, max_tokens, temperature, top_p],
@@ -85,5 +70,4 @@ iface = gr.Interface(
85
  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."
86
  )
87
 
88
- # Launch the Gradio interface
89
- iface.launch(share=False,server_name="0.0.0.0",server_port=7000)
 
1
  import subprocess
 
2
  import os
3
  import gradio as gr
4
  import json
5
  import base64
6
  from huggingface_hub import InferenceClient, login
7
 
8
+ reinstall = os.getenv("APP_REINSTALL")
9
+ if reinstall:
10
+ node_install_command = "source /home/user/.bashrc && source /home/user/.nvm/nvm.sh && nvm install --lts && npm update -g npm"
11
+ subprocess.call(["pip", "install", "--upgrade", "pip"])
12
  subprocess.call("curl -o- file:///home/user/app/data/setup.sh | bash", shell=True, executable='/bin/bash')
13
+ subprocess.Popen(node_install_command, shell=True, executable='/bin/bash').wait()
14
+ subprocess.Popen("npm install -g n8n", shell=True, executable='/bin/bash').wait()
15
+ subprocess.Popen("npm install -g forever", shell=True, executable='/bin/bash').wait()
 
 
16
  subprocess.call("curl -o- file:///home/user/app/data/config_nginx.sh | bash", shell=True, executable='/bin/bash')
17
 
18
+ subprocess.call(["forever", "start", "/home/user/.nvm/versions/node/v20.16.0/bin/n8n"])
19
+ subprocess.call(["forever", "start", "/usr/sbin/nginx"], shell=True, executable='/bin/bash')
20
 
21
  # Get the API key from environment variables
22
+ api_key = os.getenv("UCODE_SECRET")
23
+ login(api_key)
24
 
25
  # Initialize the InferenceClient with the specified model
26
  client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct")
27
 
28
  def decode_base64_to_json(base64_str):
29
  try:
 
30
  decoded_bytes = base64.b64decode(base64_str)
 
31
  decoded_str = decoded_bytes.decode('utf-8')
 
32
  decoded_str = decoded_str.replace("\\'", "'").replace('\\"', '"').replace('\\\\', '\\')
33
  print(f"===================================================\nDecoded string: {decoded_str}\n===================================================") # Log the decoded string
 
34
  return json.loads(decoded_str)
35
  except Exception as e:
36
  raise ValueError(f"Error decoding base64 to JSON: {str(e)}")
37
 
 
38
  @spaces.GPU(enable_queue=True)
39
  def chat_completion(user_input, max_tokens, temperature, top_p):
40
  try:
 
41
  input_data = decode_base64_to_json(user_input)
 
 
42
  if not isinstance(input_data, list):
43
  raise ValueError("Input must be a list of messages.")
44
 
45
  response = ""
 
 
46
  for message in client.chat_completion(
47
  input_data,
48
  max_tokens=max_tokens,
 
57
  except Exception as e:
58
  return json.dumps({"status": "error", "message": str(e)})
59
 
 
60
  user_input = gr.Textbox(label="User Input as Base64-encoded JSON String", lines=10)
61
  max_tokens = gr.Slider(minimum=1, maximum=8092, value=150, label="Max Tokens")
62
  temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.7, label="Temperature")
63
  top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.9, label="Top P")
64
 
 
65
  iface = gr.Interface(
66
  fn=chat_completion,
67
  inputs=[user_input, max_tokens, temperature, top_p],
 
70
  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."
71
  )
72
 
73
+ iface.launch(share=False, server_name="0.0.0.0", server_port=7000)