app.py
CHANGED
|
@@ -1,101 +1,107 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# Function to create a character
|
| 7 |
-
def create_character(name, age, gender, personality, backstory):
|
| 8 |
-
characters[name] = {
|
| 9 |
-
"age": age,
|
| 10 |
-
"gender": gender,
|
| 11 |
-
"personality": personality,
|
| 12 |
-
"backstory": backstory,
|
| 13 |
-
}
|
| 14 |
-
return f"Character {name} created successfully!", list(characters.keys())
|
| 15 |
-
|
| 16 |
-
# Function to handle personality-based responses
|
| 17 |
-
def respond_with_personality(character_name, message, history):
|
| 18 |
-
if character_name not in characters:
|
| 19 |
-
return f"Character {character_name} not found!", history
|
| 20 |
-
|
| 21 |
-
character = characters[character_name]
|
| 22 |
-
personality = character['personality']
|
| 23 |
-
|
| 24 |
-
if personality == "Heroic":
|
| 25 |
-
response = f"{character_name}: Fear not! With bravery, you can overcome any challenge!"
|
| 26 |
-
elif personality == "Sarcastic":
|
| 27 |
-
response = f"{character_name}: Oh really? Facing a tough day? Why not just take a nap? That usually works."
|
| 28 |
-
elif personality == "Optimistic":
|
| 29 |
-
response = f"{character_name}: Don't worry, things will turn out great! Keep your chin up!"
|
| 30 |
-
elif personality == "Empathetic":
|
| 31 |
-
response = f"{character_name}: I totally understand what you're going through. That must be really tough."
|
| 32 |
else:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# Character creation UI
|
| 54 |
-
with gr.Tab("Create Character"):
|
| 55 |
-
name_input = gr.Textbox(label="Character Name", placeholder="Enter character name")
|
| 56 |
-
age_input = gr.Number(label="Character Age", value=25, step=1)
|
| 57 |
-
gender_input = gr.Dropdown(choices=["Male", "Female", "Other"], label="Character Gender")
|
| 58 |
-
personality_input = gr.Dropdown(
|
| 59 |
-
choices=["Heroic", "Sarcastic", "Optimistic", "Empathetic"],
|
| 60 |
-
label="Character Personality"
|
| 61 |
-
)
|
| 62 |
-
backstory_input = gr.Textbox(label="Character Backstory", placeholder="Enter backstory")
|
| 63 |
-
|
| 64 |
-
create_button = gr.Button("Create Character")
|
| 65 |
-
create_output = gr.Textbox(label="Output", interactive=False)
|
| 66 |
-
character_selector = gr.Dropdown(choices=[], label="Select Character")
|
| 67 |
-
|
| 68 |
-
# Update the dropdown after character creation
|
| 69 |
-
def create_and_update(name, age, gender, personality, backstory):
|
| 70 |
-
output_message, updated_character_list = create_character(name, age, gender, personality, backstory)
|
| 71 |
-
return output_message, gr.update(choices=updated_character_list)
|
| 72 |
-
|
| 73 |
-
create_button.click(
|
| 74 |
-
create_and_update,
|
| 75 |
-
inputs=[name_input, age_input, gender_input, personality_input, backstory_input],
|
| 76 |
-
outputs=[create_output, character_selector]
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
# Chat UI
|
| 80 |
-
with gr.Tab("Chat"):
|
| 81 |
-
chat_box = gr.Chatbot(label="Chat History")
|
| 82 |
-
message_input = gr.Textbox(label="Your Message", placeholder="Type a message...")
|
| 83 |
-
|
| 84 |
-
send_button = gr.Button("Send Message")
|
| 85 |
-
|
| 86 |
-
# Define the chat interaction
|
| 87 |
-
def chat_and_clear(character_name, message, history):
|
| 88 |
-
history, response = chat(character_name, message, history)
|
| 89 |
-
return history, ""
|
| 90 |
-
|
| 91 |
-
send_button.click(chat_and_clear,
|
| 92 |
-
inputs=[character_selector, message_input, chat_box],
|
| 93 |
-
outputs=[chat_box, message_input])
|
| 94 |
-
|
| 95 |
-
return demo
|
| 96 |
-
|
| 97 |
-
# Run the application
|
| 98 |
-
demo = build_interface()
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def respond(
|
| 8 |
+
message: str,
|
| 9 |
+
history: list[tuple[str, str]],
|
| 10 |
+
system_message: str,
|
| 11 |
+
max_tokens: int,
|
| 12 |
+
temperature: float,
|
| 13 |
+
top_p: float,
|
| 14 |
+
):
|
| 15 |
+
"""
|
| 16 |
+
Generates a response using the Hugging Face Inference API.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
message (str): User's current input.
|
| 20 |
+
history (list[tuple[str, str]]): Previous conversation history.
|
| 21 |
+
system_message (str): Instructions for the model (e.g., persona details).
|
| 22 |
+
max_tokens (int): Maximum tokens allowed for the response.
|
| 23 |
+
temperature (float): Sampling temperature for randomness in the output.
|
| 24 |
+
top_p (float): Top-p (nucleus) sampling parameter.
|
| 25 |
+
|
| 26 |
+
Yields:
|
| 27 |
+
str: The generated chatbot response.
|
| 28 |
+
"""
|
| 29 |
+
messages = [{"role": "system", "content": system_message}]
|
| 30 |
+
|
| 31 |
+
for val in history:
|
| 32 |
+
if val[0]:
|
| 33 |
+
messages.append({"role": "user", "content": val[0]})
|
| 34 |
+
if val[1]:
|
| 35 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 36 |
+
|
| 37 |
+
messages.append({"role": "user", "content": message})
|
| 38 |
+
|
| 39 |
+
response = ""
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
for message in client.chat_completion(
|
| 43 |
+
messages,
|
| 44 |
+
max_tokens=max_tokens,
|
| 45 |
+
stream=True,
|
| 46 |
+
temperature=temperature,
|
| 47 |
+
top_p=top_p,
|
| 48 |
+
):
|
| 49 |
+
token = message.choices[0].delta.content
|
| 50 |
+
response += token
|
| 51 |
+
yield response
|
| 52 |
+
except Exception as e:
|
| 53 |
+
yield f"Error: {str(e)}"
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def system_message_selector(choice):
|
| 57 |
+
if choice == "Friendly Chatbot":
|
| 58 |
+
return "You are a friendly and helpful chatbot."
|
| 59 |
+
elif choice == "Professional Assistant":
|
| 60 |
+
return "You are a highly knowledgeable and professional assistant."
|
| 61 |
+
elif choice == "Curious Researcher":
|
| 62 |
+
return "You are a curious researcher who loves to explore new ideas."
|
| 63 |
+
else:
|
| 64 |
+
return "You are a helpful assistant."
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def custom_system_message(file):
|
| 68 |
+
if file:
|
| 69 |
+
return file.read().decode("utf-8")
|
| 70 |
+
return "You are a helpful assistant."
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# Create the UI components
|
| 74 |
+
system_message_radio = gr.Radio(
|
| 75 |
+
choices=["Friendly Chatbot", "Professional Assistant", "Curious Researcher"],
|
| 76 |
+
value="Friendly Chatbot",
|
| 77 |
+
label="Choose a Persona",
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
file_upload = gr.File(label="Upload Custom System Message (Optional)")
|
| 81 |
|
| 82 |
+
def combined_system_message(choice, file):
|
| 83 |
+
if file:
|
| 84 |
+
return custom_system_message(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
else:
|
| 86 |
+
return system_message_selector(choice)
|
| 87 |
+
|
| 88 |
+
# ChatInterface with dynamic system message and file input
|
| 89 |
+
demo = gr.ChatInterface(
|
| 90 |
+
respond,
|
| 91 |
+
additional_inputs=[
|
| 92 |
+
system_message_radio,
|
| 93 |
+
file_upload,
|
| 94 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 95 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 96 |
+
gr.Slider(
|
| 97 |
+
minimum=0.1,
|
| 98 |
+
maximum=1.0,
|
| 99 |
+
value=0.95,
|
| 100 |
+
step=0.05,
|
| 101 |
+
label="Top-p (nucleus sampling)",
|
| 102 |
+
),
|
| 103 |
+
],
|
| 104 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
demo.launch()
|