Download blubeAPI.py from BlueFLL/blubeai: direct link, hf CLI and curl.
- Browser
- Download file 5.56 kB
-
https://huggingface.co/spaces/BlueFLL/blubeai/resolve/main/blubeAPI.py
- Command line
-
hf download hf://spaces/BlueFLL/blubeai/blubeAPI.py
-
curl -L -o blubeAPI.py https://huggingface.co/spaces/BlueFLL/blubeai/resolve/main/blubeAPI.py
5.56 kB
| import gradio as gr | |
| import openai | |
| import json | |
| import time | |
| import sys | |
| from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper | |
| from langchain.chat_models import ChatOpenAI | |
| import gradio as gr | |
| import openai | |
| from openai import OpenAI | |
| import os | |
| import time | |
| import random | |
| import math | |
| os.environ["OPENAI_API_KEY"] = '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' | |
| client = OpenAI() | |
| def findOccurrences(s, ch): | |
| return [i for i, letter in enumerate(s) if letter == ch] | |
| def format(data: str): | |
| data = data.replace("['", "") | |
| data = data.replace("']", "") | |
| data = data.replace("[\"", "") | |
| data = data.replace("\"]", "") | |
| data = data.replace("\\\'", "\'") | |
| data = data.replace("\\n", "") | |
| data = data.replace("**", "") | |
| data = data.replace(".", ". ") | |
| data = data.replace("#", "") | |
| data = data.replace(" ", " ") | |
| sourceErrorInstances = findOccurrences(data, "ใ") | |
| lSourceErrorInstances = findOccurrences(data, "ใ") | |
| for i in range(len(sourceErrorInstances)): | |
| index = int(sourceErrorInstances[i]) | |
| lindex = int(lSourceErrorInstances[i]) | |
| data[index:lindex] = "" | |
| return data | |
| def runbot(): | |
| global iface | |
| iface = gr.Interface(fn=chatbot, | |
| inputs=gr.components.Textbox(lines=7, label="Enter your text"), | |
| outputs="text", | |
| title="Blue BotBuilders AI") | |
| global thread | |
| global my_assistant | |
| global persona | |
| print("\n\n\n\n\nSTACK and INFO:\n\n") | |
| # Get persona | |
| persona = open("data/persona.txt") | |
| persona = persona.readlines() | |
| # Get files and send them | |
| print("(1/3) Sending data to OpenAI, this may take a while...\n") | |
| up_files = [] | |
| files = client.files.create( | |
| file=open("docs/data.txt", "rb"), | |
| purpose="assistants" | |
| ) | |
| up_files.append(files.id) | |
| files = client.files.create( | |
| file=open("docs/customdata.txt", "rb"), | |
| purpose="assistants" | |
| ) | |
| up_files.append(files.id) | |
| my_assistant = client.beta.assistants.create( | |
| instructions=f"{persona}", | |
| name="Blube AI", | |
| tools=[{"type": "retrieval"}], | |
| model="gpt-3.5-turbo-1106", | |
| file_ids=up_files | |
| ) | |
| print("(2/3) Files sent, creating thread...\n") | |
| # Create a thread for the assistant | |
| thread = client.beta.threads.create() | |
| print("(3/3) Finished creating thread, launching interface...\n") | |
| iface.launch(share=False) | |
| def getAssistant(): | |
| return my_assistant | |
| def getPersona(): | |
| return persona | |
| def getThread(): | |
| return thread | |
| def chatbot(input_text): | |
| stime = time.time() | |
| my_assistant = getAssistant() | |
| persona = getPersona() | |
| thread = getThread() | |
| print("(1/4) Message sent, getting response...\n") | |
| message = client.beta.threads.messages.create( | |
| thread_id=thread.id, | |
| role="user", | |
| content=f"{input_text}" | |
| ) | |
| print("(2/4) Thread created, running thread...\n") | |
| # Run the thread | |
| run = client.beta.threads.runs.create( | |
| thread_id=thread.id, | |
| assistant_id=my_assistant.id, | |
| instructions=f"{persona}" | |
| ) | |
| print("(3/4) Thread active, getting thread...\n") | |
| # Get the thread | |
| run = client.beta.threads.runs.retrieve( | |
| thread_id=thread.id, | |
| run_id=run.id | |
| ) | |
| print("(4/4) Thread received, awaiting chat completion\n") | |
| # Wait for completion | |
| t: int = 0 | |
| tt = 0 | |
| errorCode = 0 | |
| while run.status != "completed": | |
| run = client.beta.threads.runs.retrieve( | |
| thread_id=thread.id, run_id=run.id | |
| ) | |
| s = run.status | |
| if s == "failed": | |
| print(f"Process failed after {tt} ticks at {run.failed_at} with error stack:\n{run.last_error}\n\n") | |
| errorCode = 1 | |
| break | |
| elif s == "cancelled": | |
| print(f"Process cancelled after {tt} ticks at {run.cancelled_at} with error stack:\n{run.last_error}\n\n") | |
| errorCode = 2 | |
| elif s == "in_progress" or s == "queued": | |
| t = t + 1 | |
| tt = tt + 1 | |
| else: | |
| print(s) | |
| if t > 15: | |
| print(f"Time taken is over 15 ticks, average time is 11, check issues.\n\nINFO: Status is {s} and the total ticks is {tt}") | |
| t = 0 | |
| if errorCode == 1: | |
| raise RuntimeError("\n\nThe process finished with an error: Process failed unexpectedly. Check the command prompt for more details. This error is most likely a overuse error, so the program will sleep for 20 seconds to avoid more errors\n\n") | |
| time.sleep(20) | |
| print("\n20 seconds is over: requests are up") | |
| elif errorCode == 2: | |
| raise RuntimeError("The process finished with an error: Process cancelled unexpectedly. Check the command prompt for more details.") | |
| else: | |
| messages = client.beta.threads.messages.list( | |
| thread_id=thread.id | |
| ) | |
| messages = messages.json() | |
| print(f"Process completed in {tt} ticks at {run.completed_at}") | |
| mesf = open("messages.json", "w") | |
| mesf.write(messages) | |
| mesf.close() | |
| mesf = open("./messages.json", "rt") | |
| mess = json.load(mesf) | |
| mess = mess["data"] | |
| finalMsg = [] | |
| finalMsg.append(mess[0]["content"][0]["text"]["value"]) | |
| finalMsg = str(finalMsg) | |
| finalMsg = format(finalMsg) | |
| etime = time.time() | |
| ttime = etime - stime | |
| print(f"Finished in {ttime} seconds.") | |
| return finalMsg |