-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetuned-mistral7b-monsterapi.py
57 lines (51 loc) · 2.01 KB
/
finetuned-mistral7b-monsterapi.py
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
import requests
import json
def generate_recipe(prompt):
url = "https://f40e171a-ade7-40c5-913b-84db8e5fd1f8.monsterapi.ai/generate"
api_auth_token = #AUTH TOKEN HERE
headers = {
"Authorization": f"Bearer {api_auth_token}",
"Content-Type": "application/json"
}
data = {
"prompt": f"You are an expert chef. You know about a lot of diverse cuisines. You write helpful tasty recipes.\n\n###Instruction: please think step by step and generate a detailed recipe for {prompt}\n\n###Response:",
"stream": False,
"max_tokens": 256,
"n": 1,
"best_of": 1,
"presence_penalty": 0,
"frequency_penalty": 0,
"repetition_penalty": 1,
"temperature": 0.7,
"top_p": 1,
"top_k": -1,
"min_p": 0,
"use_beam_search": False,
"length_penalty": 1,
"early_stopping": False
}
response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)
if response.status_code == 200:
response_text = response.text
start_str = "recipe name: "
end_str = "token_counts"
start_idx = response_text.find(start_str) + len(start_str)
end_idx = response_text.find(end_str)
recipe_text = response_text[start_idx:end_idx]
recipe_text = recipe_text.replace("\\n", "\n").replace('\\"', '"')
replaced_string = recipe_text.replace("\\\n", "\n")
lines = replaced_string.split('\n')
reversed_lines = list(reversed(lines))
for line in reversed_lines:
if any(char.isdigit() for char in line):
index_to_remove = lines.index(line)
lines = lines[:index_to_remove]
break
cleaned_text = '\n'.join(lines)
final_string = cleaned_text.replace("\\\n", "\n")
print(final_string)
return final_string
else:
print(f"Error: {response.status_code}, Message: {response.text}")
return "Error"
result = generate_recipe("grape spaghetti")