Envoid commited on
Commit
7b5af03
·
1 Parent(s): 6f424b1

Upload autocorpus.py

Browse files
Files changed (1) hide show
  1. autocorpus.py +83 -0
autocorpus.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import requests
4
+ import random
5
+
6
+ # Function to load presets from the "presets" folder
7
+ def load_presets():
8
+ preset_files = [f for f in os.listdir("presets") if f.endswith(".json")]
9
+ presets = {}
10
+ for preset_file in preset_files:
11
+ preset_name = os.path.splitext(preset_file)[0]
12
+ with open(os.path.join("presets", preset_file), "r", encoding="utf-8") as preset_data:
13
+ presets[preset_name] = json.load(preset_data)
14
+ return presets
15
+
16
+ # Function to select a preset or use default
17
+ def select_preset(presets):
18
+ while True:
19
+ print("Select a preset:")
20
+ for i, preset in enumerate(presets.keys()):
21
+ print(f"{i + 1}. {preset}")
22
+ choice = input("Enter the number of the preset (default: 'default'): ")
23
+ if choice.isdigit() and 1 <= int(choice) <= len(presets):
24
+ return presets[list(presets.keys())[int(choice) - 1]]
25
+ elif choice.strip() == "":
26
+ return presets["default"]
27
+ else:
28
+ print("Invalid choice. Please enter a valid number or press Enter for default.")
29
+
30
+ # Function to interact with the API
31
+ def call_api(prompt, preset, config):
32
+ url = "http://YOUR.IP.ADDRESS.HERE:5001/api/v1/generate"
33
+
34
+ with open(config, "r", encoding="utf-8") as config_file:
35
+ config_data = json.load(config_file)
36
+
37
+ data = {
38
+ "prompt": f"### Instruction:\n{prompt}\n### Response:\n",
39
+ **preset,
40
+ **config_data,
41
+ }
42
+ response = requests.post(url, json=data)
43
+
44
+ try:
45
+ response_json = response.json()
46
+ response_text = response_json.get("results", [{}])[0].get("text", "")
47
+ return response_text
48
+ except json.JSONDecodeError:
49
+ print("API response could not be decoded as JSON.")
50
+ return ""
51
+
52
+ # Function to read random words from prompt.txt
53
+ def get_random_word_from_file(file_path):
54
+ with open(file_path, "r", encoding="utf-8") as word_file:
55
+ words = word_file.read().splitlines()
56
+ return random.choice(words)
57
+
58
+ # Main loop
59
+ def main():
60
+ presets = load_presets()
61
+ preset = select_preset(presets)
62
+
63
+ file_size_limit = 50 * 1024 * 1024 # 50 megabytes
64
+ corpus_file = open("autocorpus.txt", "a", encoding="utf-8")
65
+
66
+ while True:
67
+ random_word = get_random_word_from_file("prompt.txt")
68
+ full_prompt = f"Write a slice of life scene with plenty of visual storytelling and wonderment. Use the following prompt for inspiration: {random_word}."
69
+
70
+ response = call_api(full_prompt, preset, "config.json")
71
+ print("Response:")
72
+ print(response)
73
+
74
+ # Write response to autocorpus.txt and add quadruple line breaks
75
+ corpus_file.write(response + "\n\n\n\n")
76
+ corpus_file.flush() # Ensure data is written immediately
77
+
78
+ # Check if the file size exceeds the limit
79
+ if os.path.getsize("autocorpus.txt") > file_size_limit:
80
+ break
81
+
82
+ if __name__ == "__main__":
83
+ main()