-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateQA.sh
77 lines (60 loc) · 2.41 KB
/
generateQA.sh
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
#!/bin/bash
# Set your Hugging Face API key as an environment variable or replace this with your key
HUGGINGFACE_API_KEY="${HUGGINGFACE_API_KEY:-$(read -sp "Enter Hugging Face API key: " API_KEY; echo "$API_KEY")}"
export HUGGINGFACE_API_KEY
# Set the model to use
MODEL="google/gemma-3-27b-it" # or try "another model"
# Path to the Python file containing the rule generation function
RULE_GENERATION_FILE="generateQA.py" # Replace with your file path
# Generate rules using the external Python function
RULES=$(python3 "$PYTHON_FILE" | jq -c '.rules')
# Check if rules were generated successfully
if [[ -z "$RULES" ]]; then
echo "Error: Failed to generate rules from $RULE_GENERATION_FILE"
exit 1
fi
FACTS=(
"George_Gervin isPrimaryTopicOf George_Gervin"
"George_Gervin team Chicago_Bulls"
"John_LeRoy_Hennessy wikiPageRedirects John_L._Hennessy"
)
# Python script to generate questions and answers
PYTHON_SCRIPT=$(cat <<'EOF'
import os
import sys
import json
from generateQA import generate_multihop_qa_huggingface, new_rules
def generate_qa_from_rules_and_facts(rules, facts, api_key, model):
qa_results = {}
result = generate_multihop_qa_huggingface(rule, facts, api_key, model)
if result:
question, answer = result
qa_results[rule] = (question, answer)
else:
qa_results[rule] = ("Failed to generate question/answer.", "Failed to generate question/answer.")
return qa_results
rules = json.loads(sys.argv[1])
facts = json.loads(sys.argv[2])
api_key = os.environ.get("HUGGINGFACE_API_KEY")
model = sys.argv[3]
qa_dict = generate_qa_from_rules_and_facts(rules, facts, api_key, model)
print(json.dumps(qa_dict))
EOF
)
# Convert rules and facts to JSON strings for passing to Python script
FACTS_JSON="["
for fact in "${FACTS[@]}"; do
IFS=' ' read -r subject predicate object <<< "$fact"
FACTS_JSON+="[\"$subject\", \"$predicate\", \"$object\"],"
done
FACTS_JSON="${FACTS_JSON%,}]" #Remove last comma and close array.
# Execute the Python script and capture the JSON output
JSON_OUTPUT=$(echo "$RULES" | python3 -c "$PYTHON_SCRIPT" "$RULES" "$FACTS_JSON" "$MODEL")
# Parse the JSON output and print the results
QA_DICT=$(echo "$JSON_OUTPUT" | jq -r 'to_entries[] | [.key, .value[0], .value[1]] | @tsv')
echo "$QA_DICT" | while IFS=$'\t' read -r rule question answer; do
echo "Rule: $rule"
echo "Question: $question"
echo "Answer: $answer"
echo "--------------------"
done