-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhf
More file actions
executable file
·126 lines (111 loc) · 3.04 KB
/
Copy pathhf
File metadata and controls
executable file
·126 lines (111 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
# A function to print the usage message
usage() {
echo "Usage: $0 <subcommand> [options]"
echo "Available subcommands:"
echo " text: Generate text"
echo " chat: Chat with LLM"
echo " code: Ask LLM code questions"
}
version() {
echo "0.1"
}
# check if the Hugging Face API key is set and not empty, otherwise exit the script
if [ -z "$HF_API_KEY" ]; then
# The variable is not set or is empty, exit the script
echo "The HF_API_KEY environment variable is not set, exiting the script"
exit 1
fi
BASE_URL=https://api-inference.huggingface.co
# Get the first argument as the subcommand
subcommand=$1
# Shift the subcommand from the argument list
shift
# A variable to store the text to print
model=""
temperature=1.0
candidates=1
# If the debug variable is not empty, print it
if [ -n "$debug" ]; then
echo "Using model=$model"
fi
generateText() {
local text=$1
body='{"inputs":"'"$text"'","parameters":{"temperature":'$temperature',"numReturnSequences":'$candidates'}}'
result=$(curl -s $BASE_URL/models/$model \
-H 'Authorization: Bearer '$HF_API_KEY'' \
-H 'Content-Type: application/json' \
-X POST \
-d "${body}"
)
# if result is array then take inner object
result=$(echo $result | jq 'if type=="array" then .[] else . end')
# check for errors
has_error=$(echo $result |jq 'has("error")')
if [ "$has_error" = true ]; then
echo "ERROR: $(echo $result |jq -r '.error')"
else
echo $result | jq -r '.generated_text'
fi
}
text_usage() {
echo "Usage: $0 text [-h] [-m model] [-t temperature] [-n candidates]"
echo " -h: Print this help message and exit"
echo " -m: Model to use"
echo " -t: Temperature"
echo " -n: Number of candidate responses"
}
call_text() {
# Define the valid options for the text subcommand
OPTS="hm:t:n:"
# Parse the options using getopts
while getopts $OPTS opt; do
case $opt in
m) # If the -m option is given, store the argument in the text variable
model=$OPTARG
;;
t) # If the -t option is given, store the argument in the text variable
temperature=$OPTARG
;;
n) # If the -n option is given, store the argument in the text variable
candidates=$OPTARG
;;
h)
# Handle the -h option, print the usage message and exit
text_usage
exit 0
;;
\?)
# Handle invalid options, print an error message and exit
echo "Invalid option: -$OPTARG" >&2
text_usage
exit 1
;;
esac
done
# Shift the processed options
shift $((OPTIND-1))
# if model not set then choose default text generation model
if [ -z "$model" ]; then
echo "model is not set, exiting the script"
exit 1
fi
generateText "$@"
}
# Handle the subcommand using a case statement
case $subcommand in
text)
call_text "$@"
;;
-v | --version)
version
;;
-h | --help)
usage
;;
*)
# Invalid subcommand, print an error message and exit
echo "Invalid subcommand: $subcommand" >&2
usage
;;
esac