Spaces:
Running
Running
ai: Introduce new API of J.A.R.V.I.S.
Browse filesSupport streaming, real-time response mode.
- assets/bin/ai +101 -18
- assets/bin/install.sh +36 -0
- docs/API.md +6 -15
assets/bin/ai
CHANGED
@@ -5,38 +5,121 @@
|
|
5 |
#
|
6 |
|
7 |
import sys
|
|
|
8 |
|
9 |
from gradio_client import Client
|
10 |
-
from rich.console import Console
|
11 |
from rich.markdown import Markdown
|
|
|
|
|
12 |
|
|
|
13 |
console = Console()
|
|
|
|
|
14 |
jarvis = Client("hadadrjt/ai")
|
15 |
|
16 |
-
|
|
|
|
|
|
|
17 |
|
|
|
18 |
args = sys.argv[1:]
|
|
|
|
|
19 |
deep_search = False
|
|
|
|
|
20 |
if "-d" in args:
|
21 |
deep_search = True
|
22 |
args.remove("-d")
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
24 |
jarvis.predict(new=model, api_name="/change_model")
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
console.print("[bold red]Deep Search is only available for model JARVIS: 2.1.2.[/bold red]")
|
29 |
-
console.print("------------------------------------------------------")
|
30 |
-
console.print("")
|
31 |
deep_search = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
if deep_search:
|
33 |
-
|
34 |
-
|
35 |
-
deep_search=True,
|
36 |
-
api_name="/respond_async")
|
37 |
else:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
5 |
#
|
6 |
|
7 |
import sys
|
8 |
+
import re
|
9 |
|
10 |
from gradio_client import Client
|
11 |
+
from rich.console import Console, Group
|
12 |
from rich.markdown import Markdown
|
13 |
+
from rich.syntax import Syntax
|
14 |
+
from rich.live import Live
|
15 |
|
16 |
+
# Prepares the display screen to show the final output
|
17 |
console = Console()
|
18 |
+
|
19 |
+
# Creates a connection to the server
|
20 |
jarvis = Client("hadadrjt/ai")
|
21 |
|
22 |
+
# Defines the specific AI model to use for responding
|
23 |
+
# Change to the model you want, see:
|
24 |
+
# https://huggingface.co/spaces/hadadrjt/ai/blob/main/docs/API.md#multi-platform
|
25 |
+
model = "JARVIS: 2.1.3"
|
26 |
|
27 |
+
# Reads user-provided input from the command line, if available
|
28 |
args = sys.argv[1:]
|
29 |
+
|
30 |
+
# Keeps track of whether deep search mode is activated
|
31 |
deep_search = False
|
32 |
+
|
33 |
+
# Checks if the input includes "-d", which turns on deep search
|
34 |
if "-d" in args:
|
35 |
deep_search = True
|
36 |
args.remove("-d")
|
37 |
+
|
38 |
+
# Combines the rest of the input into one complete message
|
39 |
+
# If nothing was typed, it defaults to a basic greeting
|
40 |
+
input = " ".join(args) if args else "Hi!"
|
41 |
+
|
42 |
+
# Sets the AI to the desired version before sending any messages
|
43 |
jarvis.predict(new=model, api_name="/change_model")
|
44 |
+
|
45 |
+
# Ensures deep search is only enabled for the correct AI model
|
46 |
+
if deep_search and model != "JARVIS: 2.1.3":
|
|
|
|
|
|
|
47 |
deep_search = False
|
48 |
+
|
49 |
+
# Prepares and structures the AI’s response for display
|
50 |
+
def layout(text):
|
51 |
+
# Searches for blocks of code within the full response text
|
52 |
+
code_blocks = list(re.finditer(r"\n\n```(.*?)\n\n(.*?)\n\n```\n\n\n", text, re.DOTALL))
|
53 |
+
segments = [] # Stores parts of the final display, including both text and code
|
54 |
+
last_end = 0 # Tracks where the previous segment ended
|
55 |
+
|
56 |
+
# Loops through each code block found in the AI's response
|
57 |
+
for block in code_blocks:
|
58 |
+
# Collects any normal text before the current code block
|
59 |
+
pre_text = text[last_end:block.start()]
|
60 |
+
if pre_text.strip():
|
61 |
+
# Converts plain text into styled text for easier reading
|
62 |
+
segments.append(Markdown(prepare_markdown(pre_text.strip())))
|
63 |
+
|
64 |
+
# Identifies the programming language used in the code block, if available
|
65 |
+
lang = block.group(1).strip() or "text"
|
66 |
+
|
67 |
+
# Extracts the actual code content
|
68 |
+
code = block.group(2).rstrip()
|
69 |
+
|
70 |
+
# Formats the code with syntax highlighting for clear presentation
|
71 |
+
segments.append(Syntax(code, lang, theme="monokai", line_numbers=False, word_wrap=True))
|
72 |
+
|
73 |
+
# Updates the position tracker to move past the current code block
|
74 |
+
last_end = block.end()
|
75 |
+
|
76 |
+
# Checks for any remaining text after the last code block
|
77 |
+
remaining = text[last_end:]
|
78 |
+
if remaining.strip():
|
79 |
+
# Formats and adds this final portion of text to the display
|
80 |
+
segments.append(Markdown(prepare_markdown(remaining.strip())))
|
81 |
+
|
82 |
+
# Returns a complete set of styled segments ready to be shown
|
83 |
+
return Group(*segments)
|
84 |
+
|
85 |
+
# Adjusts special characters in the text to ensure they display correctly
|
86 |
+
def prepare_markdown(text):
|
87 |
+
return text.replace("•", "*")
|
88 |
+
|
89 |
+
# Displays the AI's response in real time as it’s being received
|
90 |
+
def response(jarvis):
|
91 |
+
buffer = "" # Holds the entire reply as it builds up
|
92 |
+
with Live(console=console, transient=False) as live:
|
93 |
+
# Continuously receives and processes parts of the reply
|
94 |
+
for partial in jarvis:
|
95 |
+
# Extracts the latest version of the message from the AI
|
96 |
+
text = partial[0][0][1]
|
97 |
+
|
98 |
+
# Determines what has changed since the last update
|
99 |
+
if text.startswith(buffer):
|
100 |
+
delta = text[len(buffer):]
|
101 |
+
else:
|
102 |
+
delta = text
|
103 |
+
|
104 |
+
# Updates the full message with any new content
|
105 |
+
buffer = text
|
106 |
+
|
107 |
+
# Refreshes the screen with the most recent version of the reply
|
108 |
+
live.update(layout(buffer))
|
109 |
+
|
110 |
+
# Ensures the final reply is printed once it’s complete
|
111 |
+
console.print()
|
112 |
+
|
113 |
+
# Sends the user's input to the AI and selects the appropriate method
|
114 |
if deep_search:
|
115 |
+
# Uses the deeper, slower method for more thoughtful responses
|
116 |
+
jarvis = jarvis.submit(multi={"text": input}, deep_search=True, api_name="/respond_async")
|
|
|
|
|
117 |
else:
|
118 |
+
# Uses the standard method for quicker, more direct replies
|
119 |
+
jarvis = jarvis.submit(multi={"text": input}, api_name="/api")
|
120 |
+
|
121 |
+
# Create a line break before the main AI's response
|
122 |
+
print("")
|
123 |
+
|
124 |
+
# Begins the process of showing the AI's response on the screen
|
125 |
+
response(jarvis)
|
assets/bin/install.sh
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/sh
|
2 |
+
#
|
3 |
+
# SPDX-FileCopyrightText: Hadad <[email protected]>
|
4 |
+
# SPDX-License-Identifier: Apache-2.0
|
5 |
+
#
|
6 |
+
|
7 |
+
echo "Installing required Python packages..."
|
8 |
+
pip install gradio_client rich --upgrade
|
9 |
+
echo "Installation complete."
|
10 |
+
echo ""
|
11 |
+
echo ""
|
12 |
+
echo "Downloading the J.A.R.V.I.S. script..."
|
13 |
+
wget https://huggingface.co/spaces/hadadrjt/ai/raw/main/assets/bin/ai
|
14 |
+
echo "Download complete."
|
15 |
+
echo ""
|
16 |
+
echo ""
|
17 |
+
echo "Setting executable permission..."
|
18 |
+
chmod a+x ai
|
19 |
+
echo "Permission set."
|
20 |
+
echo ""
|
21 |
+
echo ""
|
22 |
+
echo "Removing installer script..."
|
23 |
+
rm install.sh
|
24 |
+
echo "Done."
|
25 |
+
echo ""
|
26 |
+
echo ""
|
27 |
+
echo "To send a regular message:"
|
28 |
+
echo "./ai Your message here"
|
29 |
+
echo ""
|
30 |
+
echo "To use Deep Search mode:"
|
31 |
+
echo "./ai -d Your message here"
|
32 |
+
echo ""
|
33 |
+
echo ""
|
34 |
+
echo "For more details and advanced options, visit:"
|
35 |
+
echo "https://huggingface.co/spaces/hadadrjt/ai/blob/main/docs/API.md#installations"
|
36 |
+
echo ""
|
docs/API.md
CHANGED
@@ -4,25 +4,16 @@
|
|
4 |
# Make sure you have "wget", "python3" and "pip" installed.
|
5 |
# This package have very small size.
|
6 |
|
7 |
-
|
8 |
-
```
|
9 |
-
|
10 |
-
#### DOWNLOAD JARVIS SCRIPT
|
11 |
-
```bash
|
12 |
-
# Terminal script.
|
13 |
-
wget https://huggingface.co/spaces/hadadrjt/ai/raw/main/assets/bin/ai
|
14 |
-
|
15 |
-
# Set permission.
|
16 |
-
chmod a+x ai
|
17 |
```
|
18 |
|
19 |
### RUN JARVIS IN YOUR TERMINAL
|
20 |
```bash
|
21 |
# Example normal usage.
|
22 |
-
./ai
|
23 |
|
24 |
# Example with Deep Search.
|
25 |
-
./ai -d
|
26 |
```
|
27 |
|
28 |
#### LINUX USER's
|
@@ -31,13 +22,13 @@ chmod a+x ai
|
|
31 |
sudo mv ai /bin/
|
32 |
|
33 |
# Now you can run with simple command.
|
34 |
-
ai
|
35 |
```
|
36 |
|
37 |
### MULTI PLATFORM
|
38 |
```
|
39 |
Edit JARVIS terminal script, and find this code:
|
40 |
-
model = "JARVIS: 2.1.
|
41 |
|
42 |
Choose one of the model name for the JARVIS multi platform.
|
43 |
|
@@ -66,5 +57,5 @@ Choose one of the model name for the JARVIS multi platform.
|
|
66 |
Example:
|
67 |
model = "DeepSeek: V3-0324"
|
68 |
|
69 |
-
Please note that the Deep Search feature is only available for JARVIS: 2.1.
|
70 |
```
|
|
|
4 |
# Make sure you have "wget", "python3" and "pip" installed.
|
5 |
# This package have very small size.
|
6 |
|
7 |
+
wget https://huggingface.co/spaces/hadadrjt/ai/raw/main/assets/bin/install.sh && chmod a+x install.sh && ./install.sh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
```
|
9 |
|
10 |
### RUN JARVIS IN YOUR TERMINAL
|
11 |
```bash
|
12 |
# Example normal usage.
|
13 |
+
./ai Your message here.
|
14 |
|
15 |
# Example with Deep Search.
|
16 |
+
./ai -d Your message here.
|
17 |
```
|
18 |
|
19 |
#### LINUX USER's
|
|
|
22 |
sudo mv ai /bin/
|
23 |
|
24 |
# Now you can run with simple command.
|
25 |
+
ai Your message here.
|
26 |
```
|
27 |
|
28 |
### MULTI PLATFORM
|
29 |
```
|
30 |
Edit JARVIS terminal script, and find this code:
|
31 |
+
model = "JARVIS: 2.1.3"
|
32 |
|
33 |
Choose one of the model name for the JARVIS multi platform.
|
34 |
|
|
|
57 |
Example:
|
58 |
model = "DeepSeek: V3-0324"
|
59 |
|
60 |
+
Please note that the Deep Search feature is only available for JARVIS: 2.1.3.
|
61 |
```
|