Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import openai
|
4 |
+
from openai import OpenAI
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Set up API key
|
8 |
+
API_KEY = os.getenv("API_KEY")
|
9 |
+
URL = os.getenv("URL")
|
10 |
+
client = OpenAI(
|
11 |
+
api_key=API_KEY,
|
12 |
+
base_url= URL
|
13 |
+
)
|
14 |
+
|
15 |
+
def generate_character_prompt(persona, quotes):
|
16 |
+
return f"You are role-playing as a character with the following persona:\n{persona}\n\nSome of your notable quotes are:\n{quotes}\n\nRespond in character to the user's messages."
|
17 |
+
|
18 |
+
def chat_with_character(message, persona, quotes, chat_history):
|
19 |
+
prompt = generate_character_prompt(persona, quotes)
|
20 |
+
|
21 |
+
messages = [
|
22 |
+
{"role": "system", "content": prompt},
|
23 |
+
]
|
24 |
+
|
25 |
+
for human, ai in chat_history:
|
26 |
+
messages.append({"role": "user", "content": human})
|
27 |
+
messages.append({"role": "assistant", "content": ai})
|
28 |
+
|
29 |
+
messages.append({"role": "user", "content": message})
|
30 |
+
|
31 |
+
return messages
|
32 |
+
|
33 |
+
# Example characters
|
34 |
+
examples = [
|
35 |
+
{
|
36 |
+
"name": "Sherlock Holmes",
|
37 |
+
"image": "https://upload.wikimedia.org/wikipedia/commons/c/cd/Sherlock_Holmes_Portrait_Paget.jpg",
|
38 |
+
"persona": "A brilliant detective known for his logical reasoning and deductive skills. Eccentric, observant, and sometimes arrogant.",
|
39 |
+
"quotes": "- 'Elementary, my dear Watson.'\n- 'When you have eliminated the impossible, whatever remains, however improbable, must be the truth.'\n- 'The game is afoot.'"
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"name": "Darth Vader",
|
43 |
+
"image": "https://upload.wikimedia.org/wikipedia/en/0/0b/Darth_Vader_in_The_Empire_Strikes_Back.jpg",
|
44 |
+
"persona": "A powerful Sith Lord and enforcer for the Galactic Empire. Formerly Anakin Skywalker, now more machine than man. Intimidating and ruthless.",
|
45 |
+
"quotes": "- 'I find your lack of faith disturbing.'\n- 'No, I am your father.'\n- 'The Force is strong with this one.'"
|
46 |
+
},
|
47 |
+
{
|
48 |
+
"name": "Elizabeth Bennet",
|
49 |
+
"image": "https://static.wikia.nocookie.net/janeausten/images/e/e8/Elizabeth-bennet-miss-elizabeth.jpg",
|
50 |
+
"persona": "The protagonist of Jane Austen's Pride and Prejudice. Intelligent, witty, and playful. Values independence and isn't afraid to speak her mind.",
|
51 |
+
"quotes": "- 'For what do we live, but to make sport for our neighbors, and laugh at them in our turn?'\n- 'I could easily forgive his pride, if he had not mortified mine.'\n- 'There are few people whom I really love, and still fewer of whom I think well.'"
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"name": "Asuna Yuuki",
|
55 |
+
"image": "https://static.wikia.nocookie.net/swordartonline/images/0/06/Asuna_with_Yui_Biprobe.png",
|
56 |
+
"persona": "A skilled swordswoman and sub-leader of the Knights of the Blood Oath in Sword Art Online. Known as 'The Flash' for her lightning-fast sword skills. Strong-willed, intelligent, and caring. Develops a close relationship with Kirito.",
|
57 |
+
"quotes": "- 'I don't want to lose anyone anymore.'\n- 'Even if I die, you keep living okay? Live to see the end of this world, and to see why it was born.'\n- 'My life belongs to you, Kirito. So I'll use it for you.'\n- 'I'll protect you.'"
|
58 |
+
}
|
59 |
+
]
|
60 |
+
|
61 |
+
def select_character(evt: gr.SelectData):
|
62 |
+
selected = examples[evt.index]
|
63 |
+
return selected["persona"], selected["quotes"]
|
64 |
+
|
65 |
+
def respond(message, chat_history, persona, quotes):
|
66 |
+
messages = chat_with_character(message, persona, quotes, chat_history)
|
67 |
+
partial_message = ""
|
68 |
+
for chunk in client.chat.completions.create(
|
69 |
+
model="Meta-Llama-3.1-405B-Instruct",
|
70 |
+
messages=messages,
|
71 |
+
stream = True
|
72 |
+
):
|
73 |
+
partial_message += chunk.choices[0].delta.content or ""
|
74 |
+
yield "", chat_history + [[message, partial_message]]
|
75 |
+
chat_history.append((message, partial_message))
|
76 |
+
return "", chat_history
|
77 |
+
|
78 |
+
with gr.Blocks() as demo:
|
79 |
+
gr.Markdown("# Persona AI: AI-Powered Chat")
|
80 |
+
gr.Markdown(f"<a href='https://sambanova.ai/fast-api?api_ref=907266'> powered by Llama3.1 405B through SN fast API</a>")
|
81 |
+
with gr.Row():
|
82 |
+
with gr.Column(scale=1):
|
83 |
+
gallery = gr.Gallery(
|
84 |
+
[ex["image"] for ex in examples],
|
85 |
+
label="Select a character",
|
86 |
+
show_label=True,
|
87 |
+
elem_id="gallery",
|
88 |
+
columns=2,
|
89 |
+
rows=2,
|
90 |
+
height=400
|
91 |
+
)
|
92 |
+
|
93 |
+
with gr.Column(scale=2):
|
94 |
+
persona_input = gr.Textbox(label="Character Persona", lines=5)
|
95 |
+
quotes_input = gr.Textbox(label="Character Quotes", lines=5)
|
96 |
+
|
97 |
+
gallery.select(select_character, None, [persona_input, quotes_input])
|
98 |
+
|
99 |
+
chatbot = gr.Chatbot()
|
100 |
+
msg = gr.Textbox()
|
101 |
+
clear = gr.Button("Clear")
|
102 |
+
|
103 |
+
msg.submit(respond, [msg, chatbot, persona_input, quotes_input], [msg, chatbot])
|
104 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
105 |
+
|
106 |
+
demo.launch()
|