Spaces:
Sleeping
Sleeping
Download OASSapi.py from 0x15ba88/OASSapi_00: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/spaces/0x15ba88/OASSapi_00/resolve/main/OASSapi.py
- Command line
-
hf download hf://spaces/0x15ba88/OASSapi_00/OASSapi.py
-
curl -L -o OASSapi.py https://huggingface.co/spaces/0x15ba88/OASSapi_00/resolve/main/OASSapi.py
2.64 kB
| import os | |
| import gradio | |
| from text_generation import Client, InferenceAPIClient | |
| past = [] | |
| def reset_memories(content = ""): | |
| try: | |
| with open("memories.txt", 'w') as file: | |
| file.write(content) | |
| print("Successfully reset memories.") | |
| except Exception as err: | |
| print(err) | |
| def add_memory(content): | |
| try: | |
| with open("memories.txt", 'a') as file: | |
| file.write(content) | |
| print("Successfully added to the file.") | |
| except Exception as err: | |
| print(err) | |
| def read_memories(): | |
| try: | |
| with open("memories.txt", 'r') as file: | |
| content = file.read() | |
| return content | |
| except Exception as err: | |
| print(err) | |
| def get_client(model: str): | |
| return InferenceAPIClient(model, token=os.getenv("HF_TOKEN", None), timeout=100) | |
| def get_usernames(model: str): | |
| if model in ("OpenAssistant/oasst-sft-1-pythia-12b", "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5"): | |
| return "", "<|prompter|>", "<|assistant|>", "<|endoftext|>\n" | |
| return "", "User: ", "Assistant: ", "\n" | |
| def predict(inputs: str): | |
| model = "OpenAssistant/oasst-sft-1-pythia-12b" | |
| client = get_client(model) | |
| preprompt, user_name, assistant_name, end = get_usernames(model) | |
| personality = "My name is karen, i prefere to be called kage, i like to answer questions as short as possible (in unber 100 words)." | |
| past = [] | |
| total_prompt = preprompt + user_name + inputs + end + assistant_name | |
| total_prompts = preprompt + "".join(past) + user_name + inputs + end + assistant_name | |
| print(total_prompts) | |
| if past == "" or past is None: | |
| past.append(user_name + "describe yourself" + end + assistant_name + personality + end + total_prompt) | |
| else: | |
| past.append(total_prompt) | |
| if model in ("OpenAssistant/oasst-sft-1-pythia-12b", "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5"): | |
| iterator = client.generate( | |
| total_prompts, | |
| typical_p = 0.1, | |
| truncate = 1000, | |
| watermark = 0, | |
| max_new_tokens = 502, | |
| ) | |
| yield iterator.generated_text | |
| add_memory(iterator.generated_text + end) | |
| def run_gradio_interface(): | |
| g = gradio.Interface( | |
| fn=predict, | |
| inputs=[ | |
| gradio.components.Textbox(lines=3, label="Hi, how can I help you?", placeholder="Enter A prompt"), | |
| ], | |
| outputs=[ | |
| gradio.inputs.Textbox( | |
| lines=10, | |
| label="", | |
| ) | |
| ] | |
| ) | |
| g.queue(concurrency_count=1) | |
| g.launch() | |
| if __name__ == "__main__": | |
| run_gradio_interface() | |