Spaces:
Runtime error
Runtime error
File size: 2,350 Bytes
b23917e e45d816 b23917e e45d816 b23917e e45d816 b23917e e45d816 b23917e e45d816 b23917e e45d816 b23917e |
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 |
import openai
from concurrent.futures import ThreadPoolExecutor
import streamlit as st
import os
# Add your own OpenAI API key
# openai.api_key = "sk-XXXXXXXXXXXXXXXXXXXXX"
openai.api_key = os.environ["OPENAI_API_KEY"]
def load_text(file_path):
with open(file_path, 'r') as file:
return file.read()
def save_to_file(responses, output_file):
with open(output_file, 'w') as file:
for response in responses:
file.write(response + '\n')
def call_openai_api(chunk):
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "PASS IN ANY ARBITRARY SYSTEM VALUE TO GIVE THE AI AN IDENITY"},
{"role": "user", "content": f"OPTIONAL PREPROMPTING FOLLOWING BY YOUR DATA TO PASS IN: {chunk}."},
],
max_tokens=1750,
n=1,
stop=None,
temperature=0.5,
)
return response.choices[0]['message']['content'].strip()
def split_into_chunks(text, tokens=1500):
words = text.split()
chunks = [' '.join(words[i:i + tokens]) for i in range(0, len(words), tokens)]
return chunks
def process_chunks(input_text):
chunks = split_into_chunks(input_text)
# Processes chunks in parallel
with ThreadPoolExecutor() as executor:
responses = list(executor.map(call_openai_api, chunks))
return "\n".join(responses)
def main():
st.title("InfiniteGPT")
st.write("Input unlimited size text and process it using the GPT-4 OpenAI API.")
st.write("For more details and credit check this [GitHub repository](https://github.com/emmethalm/infiniteGPT).")
# Define columns for input and output
col1, col2 = st.columns(2)
with col1:
input_text = st.text_area("Enter or paste your text here", height=300)
process_button = st.button("Process Text")
# Initialize output_text as an empty string
output_text = ''
# Process text if the button is pressed
if process_button and input_text:
with st.spinner("Processing text..."):
output_text = process_chunks(input_text)
with col2:
st.text_area("Output", output_text, height=300)
clear_button = st.button("Clear Text")
if clear_button:
input_text = ''
st.experimental_rerun()
if __name__ == "__main__":
main()
|