Spaces:
Runtime error
Runtime error
| ```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 | |
| """) |