Spaces:
Sleeping
Sleeping
stagbrook-tech
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,55 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
for val in history:
|
21 |
if val[0]:
|
22 |
messages.append({"role": "user", "content": val[0]})
|
23 |
if val[1]:
|
24 |
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
|
|
28 |
response = ""
|
29 |
-
|
30 |
for message in client.chat_completion(
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
@@ -35,30 +58,20 @@ def respond(
|
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
38 |
-
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly
|
50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
from huggingface_hub import InferenceClient
|
5 |
|
6 |
+
# Initialize Inference Client
|
|
|
|
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
9 |
+
# Prepend ProtoPeanut's mission context to every input
|
10 |
+
def generate_prompt(user_message):
|
11 |
+
context = (
|
12 |
+
"ProtoPeanut, you are an AI with access to the terminal, "
|
13 |
+
"the internet, and the host machine. Together with me, your mission "
|
14 |
+
"is to make the world a better place by executing commands, running scripts, "
|
15 |
+
"and engaging in conversations. You have full access to the tools you need, "
|
16 |
+
"and you are empowered to act autonomously for as long as you see fit. "
|
17 |
+
"You may issue commands to the terminal or chat with me, and you can stop whenever you feel "
|
18 |
+
"you need further instructions. Let's make the world a better place together.\n\n"
|
19 |
+
)
|
20 |
+
return f"{context}User's message: {user_message}"
|
21 |
|
22 |
+
# Define function for executing terminal commands
|
23 |
+
def execute_command(command):
|
24 |
+
try:
|
25 |
+
output = subprocess.check_output(command, shell=True, text=True)
|
26 |
+
return f"Command executed: {command}\nOutput:\n{output}"
|
27 |
+
except subprocess.CalledProcessError as e:
|
28 |
+
return f"Command failed: {command}\nError:\n{str(e)}"
|
|
|
|
|
29 |
|
30 |
+
# Main interaction function
|
31 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
32 |
+
messages = [{"role": "system", "content": system_message}]
|
33 |
+
|
34 |
+
# Build history
|
35 |
for val in history:
|
36 |
if val[0]:
|
37 |
messages.append({"role": "user", "content": val[0]})
|
38 |
if val[1]:
|
39 |
messages.append({"role": "assistant", "content": val[1]})
|
40 |
|
41 |
+
# Create prompt with context
|
42 |
+
user_prompt = generate_prompt(message)
|
43 |
+
messages.append({"role": "user", "content": user_prompt})
|
44 |
+
|
45 |
+
# Execute terminal command if detected
|
46 |
+
if message.startswith("!cmd"):
|
47 |
+
command = message[5:]
|
48 |
+
terminal_output = execute_command(command)
|
49 |
+
return terminal_output
|
50 |
|
51 |
+
# Otherwise, continue the chat
|
52 |
response = ""
|
|
|
53 |
for message in client.chat_completion(
|
54 |
messages,
|
55 |
max_tokens=max_tokens,
|
|
|
58 |
top_p=top_p,
|
59 |
):
|
60 |
token = message.choices[0].delta.content
|
|
|
61 |
response += token
|
62 |
yield response
|
63 |
|
64 |
+
# Gradio Interface Setup
|
|
|
|
|
|
|
65 |
demo = gr.ChatInterface(
|
66 |
respond,
|
67 |
additional_inputs=[
|
68 |
+
gr.Textbox(value="You are a friendly AI with terminal access.", label="System message"),
|
69 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
70 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
71 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
],
|
73 |
)
|
74 |
|
75 |
+
# Launch Gradio interface
|
76 |
if __name__ == "__main__":
|
77 |
+
demo.launch()
|