abenkbp commited on
Commit
6ac675b
·
verified ·
1 Parent(s): 1cc09a5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import os
3
+ import gradio as gr
4
+ import json
5
+ import base64
6
+ from huggingface_hub import InferenceClient, login
7
+
8
+ # Get the API key from environment variables
9
+ key = "UCODE_SECRET"
10
+ login(os.getenv(key))
11
+
12
+ # Initialize the InferenceClient with the specified model
13
+ client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct")
14
+
15
+ def decode_base64_to_json(base64_str):
16
+ try:
17
+ # Decode the base64 string
18
+ decoded_bytes = base64.b64decode(base64_str)
19
+ # Convert bytes to string
20
+ decoded_str = decoded_bytes.decode('utf-8')
21
+ # Fix escaped characters
22
+ decoded_str = decoded_str.replace("\\'", "'").replace('\\"', '"').replace('\\\\', '\\')
23
+ print(f"===================================================\nDecoded string: {decoded_str}\n===================================================") # Log the decoded string
24
+ # Parse the JSON string
25
+ return json.loads(decoded_str)
26
+ except Exception as e:
27
+ raise ValueError(f"Error decoding base64 to JSON: {str(e)}")
28
+
29
+
30
+ @spaces.GPU(enable_queue=True)
31
+ def chat_completion(user_input, max_tokens, temperature, top_p):
32
+ try:
33
+ # Decode the base64-encoded JSON input
34
+ input_data = decode_base64_to_json(user_input)
35
+
36
+ # Ensure the input is a list of messages
37
+ if not isinstance(input_data, list):
38
+ raise ValueError("Input must be a list of messages.")
39
+
40
+ response = ""
41
+
42
+ # Generate chat completion
43
+ for message in client.chat_completion(
44
+ input_data,
45
+ max_tokens=max_tokens,
46
+ stream=True,
47
+ temperature=temperature,
48
+ top_p=top_p,
49
+ ):
50
+ token = message.choices[0].delta.get("content", "")
51
+ response += token
52
+
53
+ return json.dumps({"status": "success", "output": response})
54
+ except Exception as e:
55
+ return json.dumps({"status": "error", "message": str(e)})
56
+
57
+ # Create Gradio components for user input
58
+ user_input = gr.Textbox(label="User Input as Base64-encoded JSON String", lines=10)
59
+ max_tokens = gr.Slider(minimum=1, maximum=8092, value=150, label="Max Tokens")
60
+ temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.7, label="Temperature")
61
+ top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.9, label="Top P")
62
+
63
+ # Set up Gradio interface
64
+ iface = gr.Interface(
65
+ fn=chat_completion,
66
+ inputs=[user_input, max_tokens, temperature, top_p],
67
+ outputs="text",
68
+ title="UCode Agent",
69
+ 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."
70
+ )
71
+
72
+ # Launch the Gradio interface
73
+ iface.launch()