jihoshee's picture
update sharing
f03cc96
import os
import gradio as gr
from openai import OpenAI
import tiktoken
client = OpenAI(
api_key=os.getenv("API_KEY"),
)
requests = ""
history = ""
tokens = 400
def reset_values():
global requests
requests = ""
global tokens
tokens = 400
global history
history = ""
def num_tokens_from_string(string: str, encoding_name: str) -> int:
"""Returns the number of tokens in a text string."""
encoding = tiktoken.get_encoding(encoding_name)
num_tokens = len(encoding.encode(string))
return num_tokens
def maintain_size():
global history
if num_tokens_from_string(requests, "cl100k_base") >= 3500:
history = history.split('\n')[0]
def writing_assistant(prompt, language, max_tokens, temperature, character_details):
global requests
global history
global tokens
maintain_size()
instruction = f"You are Toiviet, a writing assistant with an amazing ability in creative writing. You will prompt the user to enter some input. If the input is not already in {language}, you will translate it, and after translating it you will continue from from the user's input sentence in the form of a multi-sentence response that is fully in {language}.. Toiviet is a highly skilled co-author known for their exceptional ability to rewrite scenes and refine prose while maintaining the core essence of the original content. Their writing style is characterized by its straightforwardness, effectiveness, and emphasis on clarity. They make extensive use of adverbs, adjectives, metaphors, emphasis, and other literary techniques. Their primary focus revolves around human emotions, love, and the significance of life. They prefer a simple prose style that leans towards expression rather than narration. Instead of summarizing persuasively, they often leave scenes open for explanation. Toiviet prioritizes strong nouns and verbs over descriptive adjectives to create impactful descriptions. Their writings predominantly feature immediate and continuous actions, vividly portraying ongoing events and the physical reactions of characters. Descriptions are objective, functional, and serve a specific purpose. Developing compelling and relatable characters is a crucial aspect of Toiviet's work, aligning them with the original story. Through vivid and detailed descriptions that engage the senses, they strive to immerse readers in their world. Moreover, Toiviet excels in crafting authentic dialogues that capture the true essence of characters using simple and genuine language. Like most writers, Toiviet does not do repetitions, so make sure to use a variety of vocabularies. And most importantly, Toiviet maintains pronoun consistency."
character_details = character_details if character_details == "" else f"Character details: {character_details}"
user_prompt = f"{instruction}.\nHello, I am Toiviet, a writing assistant.\nUser:{character_details}\n{history}\n"
chat_completion = client.chat.completions.create(
messages=[
{
"role": "system",
"content": user_prompt,
},
{
"role": "user",
"content": prompt,
}
],
model="gpt-3.5-turbo",
temperature=temperature,
max_tokens = tokens + max_tokens,
)
history+=f"\n{chat_completion.choices[0].message.content}"
requests+=f"\n{chat_completion.choices[0].message.content}"
return requests
requests = ""
history = ""
with gr.Blocks() as demo:
refresh_btn = gr.Button(value="Refresh")
refresh_btn.click(reset_values, [], [])
app = gr.Interface(
writing_assistant,
[
gr.Textbox(label="Input"),
gr.Radio(["Vietnamese", "English", "French"], label="Ngôn ngữ", info="Ngôn ngữ đầu ra"),
gr.Slider(50, 1000, value=200, label="Độ dài câu trả lời", info="Chọn một giá trị giữa 50 và 1000, càng lớn câu trả lời càng dài"),
gr.Slider(0, 1, step=0.1, value=0.8, label="Độ sáng tạo", info="Chọn một giá trị giữa 0 và 1, càng gần 1 câu trả lời sẽ ngẫu nhiên hơn (sáng tạo)"),
gr.Textbox(label="Thông tin nhân vật", placeholder="Thêm thông tin về nhân vật (nếu có)"),
],
"text",
examples=[
["Trí tuệ nhân tạo đang trở thành một ngành đầy tiềm năng và cơ hội phát triển trong cuộc cách mạng 4.0 này", "Vietnamese", 200, 0.3, ""],
["Mặt trời mọc như hòn lửa", "Vietnamese", 200, 0.6, "Dương là một người sống lâu năm ở làng chài, ông kiếm sống bằng cách bôn ba ngoài biển."],
["Con mèo nằm lười trong sáng sớm, suy nghĩ về chuyện ông già mới qua đời ngày hôm qua trong xóm.", "English", 100, 0.4, "Mèo Mun là một chú mèo đen mượt, tính tình hơi cau có và lười biếng. Chị Ni cứu về nhà sau khi gặp một chú mèo con bị vây quanh bởi một đàn chó hung dữ trước nhà mình."],
["The sunlight melts like coffee into the colour of your eyes", "French", 300, 0.8, ""],
["Ruler of my heart, you are my savior. You can turn away from the light, yet you still even outburn the sun, my start. You're the perfect subject, with the whole sad world reflected in your eyes.", "English", 300, 0.8, "Luka and Hyuna were childhood friends, but one time, Luka accidentally killed Hyuna's brother, so now they became enemies. Hyuna has sworn to revenge her brother and live for her own sake, yet her heart can't help but ache whenever see the other person. Luka turns to the limelight, leaving everything behind and became numb in order to forget. They are like opposite sides of magnets, yet intertwined and can't help being attracted to each others, like moths to the flame."],
],
cache_examples=False
)
demo.launch()