File size: 2,962 Bytes
7b5af03 |
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 |
import os
import json
import requests
import random
# Function to load presets from the "presets" folder
def load_presets():
preset_files = [f for f in os.listdir("presets") if f.endswith(".json")]
presets = {}
for preset_file in preset_files:
preset_name = os.path.splitext(preset_file)[0]
with open(os.path.join("presets", preset_file), "r", encoding="utf-8") as preset_data:
presets[preset_name] = json.load(preset_data)
return presets
# Function to select a preset or use default
def select_preset(presets):
while True:
print("Select a preset:")
for i, preset in enumerate(presets.keys()):
print(f"{i + 1}. {preset}")
choice = input("Enter the number of the preset (default: 'default'): ")
if choice.isdigit() and 1 <= int(choice) <= len(presets):
return presets[list(presets.keys())[int(choice) - 1]]
elif choice.strip() == "":
return presets["default"]
else:
print("Invalid choice. Please enter a valid number or press Enter for default.")
# Function to interact with the API
def call_api(prompt, preset, config):
url = "http://YOUR.IP.ADDRESS.HERE:5001/api/v1/generate"
with open(config, "r", encoding="utf-8") as config_file:
config_data = json.load(config_file)
data = {
"prompt": f"### Instruction:\n{prompt}\n### Response:\n",
**preset,
**config_data,
}
response = requests.post(url, json=data)
try:
response_json = response.json()
response_text = response_json.get("results", [{}])[0].get("text", "")
return response_text
except json.JSONDecodeError:
print("API response could not be decoded as JSON.")
return ""
# Function to read random words from prompt.txt
def get_random_word_from_file(file_path):
with open(file_path, "r", encoding="utf-8") as word_file:
words = word_file.read().splitlines()
return random.choice(words)
# Main loop
def main():
presets = load_presets()
preset = select_preset(presets)
file_size_limit = 50 * 1024 * 1024 # 50 megabytes
corpus_file = open("autocorpus.txt", "a", encoding="utf-8")
while True:
random_word = get_random_word_from_file("prompt.txt")
full_prompt = f"Write a slice of life scene with plenty of visual storytelling and wonderment. Use the following prompt for inspiration: {random_word}."
response = call_api(full_prompt, preset, "config.json")
print("Response:")
print(response)
# Write response to autocorpus.txt and add quadruple line breaks
corpus_file.write(response + "\n\n\n\n")
corpus_file.flush() # Ensure data is written immediately
# Check if the file size exceeds the limit
if os.path.getsize("autocorpus.txt") > file_size_limit:
break
if __name__ == "__main__":
main()
|