Spaces:
Runtime error
Runtime error
File size: 3,916 Bytes
69c42e0 8b19a34 69c42e0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
```python
import gradio as gr
import random
import time
def chatbot(message, history, system_prompt):
# Simple response generation based on the system prompt and user message
if not system_prompt:
system_prompt = "I am a helpful AI assistant."
# Simulate typing with a stream
for i in range(1, 11):
thinking_time = random.uniform(0.1, 0.3)
time.sleep(thinking_time)
# Generate response gradually based on system prompt
if "friendly" in system_prompt.lower():
response_parts = [
"Hi there! ",
"I'd be happy to help you with that. ",
f"You asked about '{message}'. ",
"Based on what I know, I think I can provide some guidance on this topic."
]
elif "expert" in system_prompt.lower():
response_parts = [
"Based on my expertise, ",
f"regarding '{message}', ",
"I can provide you with a detailed analysis. ",
"Here's what you should know about this subject from a professional perspective."
]
elif "funny" in system_prompt.lower():
response_parts = [
"Well, well, well! ",
f"So you want to know about '{message}'? ",
"That's quite an interesting question! ",
"Let me share my thoughts with a touch of humor."
]
else:
response_parts = [
"Thank you for your question. ",
f"Regarding '{message}', ",
"I can provide the following information: ",
"Please let me know if you need any clarification or have follow-up questions."
]
partial_response = "".join(response_parts[:int(i * len(response_parts) / 10)])
yield partial_response
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🤖 Custom AI Chatbot")
gr.Markdown("Configure your chatbot's personality and knowledge using the system prompt.")
with gr.Row():
with gr.Column(scale=2):
chatbot = gr.ChatInterface(
fn=chatbot,
additional_inputs=[
gr.Textbox(
placeholder="Enter system prompt here...",
label="System Prompt",
info="Define your chatbot's personality, knowledge, and style",
lines=5
)
],
examples=[
["What's the weather like today?"],
["Can you tell me about quantum physics?"],
["Write a short poem about nature."]
],
title="Chat with Your Custom AI",
)
with gr.Column(scale=1):
gr.Markdown("## System Prompt Examples")
system_prompt_examples = gr.Examples(
examples=[
"You are a friendly assistant who speaks casually and uses simple language.",
"You are an expert in technology with deep knowledge of AI, machine learning, and computer science.",
"You are a funny comedian who responds with humor and wit.",
"You are a professional business consultant providing concise, valuable advice.",
"You are a supportive tutor who explains complex concepts in simple terms."
],
inputs=chatbot.additional_inputs[0],
label="Click on an example to use it"
)
gr.Markdown("""
### Tips for System Prompts:
- Define personality traits
- Specify knowledge domains
- Set the tone and speaking style
- Include specific instructions
- Add any constraints or limitations
""") |