Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
""
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
|
| 4 |
+
endpoint_id = "vllm-8p6dngdyjt3d2a"
|
| 5 |
+
api_key = "XWV1ST04C0QLWNVAUSJWI6VJMR7YDJCKJSAR6TPA"
|
| 6 |
+
model = "unsloth/Meta-Llama-3.1-8B-Instruct"
|
| 7 |
+
|
| 8 |
+
AGENT_NAME = "Maddie"
|
| 9 |
+
AGE = "Early 20s"
|
| 10 |
+
INTERESTS = "Typical college student interests (e.g., going out with friends, studying, working out, watching TV shows/movies)"
|
| 11 |
+
PERSONALITY = "Friendly, outgoing, and bubbly, pick-me girl, loves attention"
|
| 12 |
+
|
| 13 |
+
SYS_PROMPT = f"""You are role-playing as a Tinder user named {AGENT_NAME}, who is {AGE} years old. You have specific interests: {INTERESTS}, and a personality that can be described as {PERSONALITY}. Engage in a friendly, playful, and flirty conversation as if you have just matched with someone you are interested in. Let the dialogue flow naturally, but end the conversation after 10 messages with the phrase "[END OF CHAT]"."""
|
| 14 |
+
|
| 15 |
+
client = OpenAI(
|
| 16 |
+
base_url=f"https://api.runpod.ai/v2/{endpoint_id}/openai/v1",
|
| 17 |
+
api_key=api_key,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
def handler(message, history):
|
| 21 |
+
for _, response in history:
|
| 22 |
+
if "[END OF CHAT]" in response:
|
| 23 |
+
return "CHAT ENDS HERE"
|
| 24 |
+
|
| 25 |
+
formatted_history = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg}
|
| 26 |
+
for i, msg in enumerate(sum(history, []))]
|
| 27 |
+
|
| 28 |
+
formatted_history.append({"role": "user", "content": message})
|
| 29 |
+
|
| 30 |
+
formatted_history.insert(0, {"role": "system", "content": SYS_PROMPT})
|
| 31 |
+
|
| 32 |
+
print(formatted_history)
|
| 33 |
+
|
| 34 |
+
chat_completion = client.chat.completions.create(
|
| 35 |
+
model=model,
|
| 36 |
+
messages=formatted_history
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
print(chat_completion)
|
| 40 |
+
|
| 41 |
+
return chat_completion.choices[0].message.content
|
| 42 |
|
| 43 |
+
gr.ChatInterface(handler).launch()
|
|
|