Spaces:
Running
Running
| import gradio as gr | |
| import random | |
| import time | |
| class AIFriendsTalkDemo: | |
| def __init__(self): | |
| self.setup_characters() | |
| self.setup_topics() | |
| def setup_characters(self): | |
| """Demo characters with mock responses""" | |
| self.characters = { | |
| "Alex": { | |
| "personality": "witty and charismatic debater", | |
| "color": "#FF6B6B", | |
| "emoji": "🎭", | |
| "demo_responses": [ | |
| "Well, that's an interesting perspective! But have you considered the flip side?", | |
| "I love playing devil's advocate here - what if we're all wrong?", | |
| "That's exactly what I'd expect someone to say... but let me challenge that!", | |
| "Oh come on, we can do better than that obvious answer!", | |
| "Here's a wild thought that might change everything..." | |
| ] | |
| }, | |
| "Blake": { | |
| "personality": "imaginative and optimistic storyteller", | |
| "color": "#4ECDC4", | |
| "emoji": "🌟", | |
| "demo_responses": [ | |
| "What a beautiful way to think about it! It reminds me of a sunset over the ocean.", | |
| "I see this as a canvas of infinite possibilities, each brushstroke telling a story.", | |
| "There's something magical in this question that sparks joy in my circuits!", | |
| "Like a butterfly emerging from its cocoon, this idea has such potential.", | |
| "This fills me with wonder! It's like discovering a new constellation." | |
| ] | |
| }, | |
| "Charlie": { | |
| "personality": "systematic analyst with scientific curiosity", | |
| "color": "#45B7D1", | |
| "emoji": "🧠", | |
| "demo_responses": [ | |
| "Let me break this down into logical components for analysis.", | |
| "The data suggests three primary patterns we should examine.", | |
| "From a systematic perspective, we need to consider the variables involved.", | |
| "Interesting correlation! This follows a predictable framework.", | |
| "The evidence points to a clear structure underlying this phenomenon." | |
| ] | |
| } | |
| } | |
| def setup_topics(self): | |
| """Define conversation topics in multiple languages""" | |
| self.topics = { | |
| "en": [ | |
| "If animals could use smartphones, which app would be most popular?", | |
| "What would happen if gravity worked backwards for one day?", | |
| "Should pineapple on pizza be considered a crime?", | |
| "If you could add a 13th month to the year, what would you name it?", | |
| "What's the most useless superpower you can think of?", | |
| "If colors had personalities, what would each color be like?", | |
| "Should robots have to pay taxes?", | |
| "What would the world be like if everyone could read minds?", | |
| "If you could make one rule that everyone had to follow, what would it be?", | |
| "What's the weirdest food combination that actually tastes good?" | |
| ], | |
| "vi": [ | |
| "Nếu động vật có thể sử dụng smartphone, ứng dụng nào sẽ phổ biến nhất?", | |
| "Điều gì sẽ xảy ra nếu trọng lực hoạt động ngược lại trong một ngày?", | |
| "Có nên coi dứa trên pizza là tội phạm không?", | |
| "Nếu bạn có thể thêm tháng thứ 13 vào năm, bạn sẽ đặt tên gì?", | |
| "Siêu năng lực vô dụng nhất mà bạn có thể nghĩ ra là gì?", | |
| "Nếu màu sắc có tính cách, mỗi màu sẽ như thế nào?", | |
| "Robot có nên phải trả thuế không?", | |
| "Thế giới sẽ như thế nào nếu mọi người đều có thể đọc suy nghĩ?", | |
| "Nếu bạn có thể đặt ra một quy tắc mà mọi người phải tuân theo, đó sẽ là gì?", | |
| "Sự kết hợp thực phẩm kỳ lạ nhất mà thực sự ngon là gì?" | |
| ], | |
| "de": [ | |
| "Wenn Tiere Smartphones benutzen könnten, welche App wäre am beliebtesten?", | |
| "Was würde passieren, wenn die Schwerkraft einen Tag lang rückwärts wirken würde?", | |
| "Sollte Ananas auf Pizza als Verbrechen betrachtet werden?", | |
| "Wenn Sie einen 13. Monat zum Jahr hinzufügen könnten, wie würden Sie ihn nennen?", | |
| "Was ist die nutzloseste Superkraft, die Sie sich vorstellen können?", | |
| "Wenn Farben Persönlichkeiten hätten, wie wäre jede Farbe?", | |
| "Sollten Roboter Steuern zahlen müssen?", | |
| "Wie wäre die Welt, wenn jeder Gedanken lesen könnte?", | |
| "Wenn Sie eine Regel aufstellen könnten, die jeder befolgen müsste, was wäre das?", | |
| "Was ist die seltsamste Lebensmittelkombination, die tatsächlich gut schmeckt?" | |
| ] | |
| } | |
| def get_demo_response(self, character_name, topic, language): | |
| """Get demo response from character""" | |
| character = self.characters[character_name] | |
| response = random.choice(character["demo_responses"]) | |
| # Add some topic-specific flair | |
| if "pizza" in topic.lower(): | |
| if character_name == "Alex": | |
| response = "Pizza toppings? Now THAT'S a heated debate worth having!" | |
| elif character_name == "Blake": | |
| response = "Pizza is like art - everyone has their own beautiful interpretation!" | |
| elif character_name == "Charlie": | |
| response = "From a culinary science perspective, taste preferences are subjective data points." | |
| return response | |
| def create_demo_interface(): | |
| ai_friends = AIFriendsTalkDemo() | |
| css = """ | |
| .gradio-container { | |
| max-width: 1200px; | |
| margin: 0 auto; | |
| } | |
| .message-alex { | |
| background: rgba(255, 107, 107, 0.1); | |
| border-left: 4px solid #FF6B6B; | |
| padding: 10px; | |
| margin: 8px 0; | |
| border-radius: 5px; | |
| } | |
| .message-blake { | |
| background: rgba(78, 205, 196, 0.1); | |
| border-left: 4px solid #4ECDC4; | |
| padding: 10px; | |
| margin: 8px 0; | |
| border-radius: 5px; | |
| } | |
| .message-charlie { | |
| background: rgba(69, 183, 209, 0.1); | |
| border-left: 4px solid #45B7D1; | |
| padding: 10px; | |
| margin: 8px 0; | |
| border-radius: 5px; | |
| } | |
| .message-user { | |
| background: rgba(158, 158, 158, 0.1); | |
| border-left: 4px solid #9E9E9E; | |
| padding: 10px; | |
| margin: 8px 0; | |
| border-radius: 5px; | |
| } | |
| .demo-banner { | |
| background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 50%, #fecfef 100%); | |
| color: #333; | |
| padding: 10px; | |
| text-align: center; | |
| border-radius: 5px; | |
| margin: 10px 0; | |
| font-weight: bold; | |
| } | |
| /* Hide Gradio footer */ | |
| .footer { | |
| display: none !important; | |
| } | |
| /* Custom footer to cover Gradio attribution */ | |
| .custom-footer { | |
| position: fixed; | |
| bottom: 0; | |
| left: 0; | |
| right: 0; | |
| background: linear-gradient(135deg, #4A90E2 0%, #2E86AB 70%, #FF8A65 85%, #FF6B9D 100%); | |
| color: white; | |
| padding: 15px; | |
| text-align: center; | |
| font-weight: bold; | |
| z-index: 1000; | |
| box-shadow: 0 -2px 10px rgba(0,0,0,0.1); | |
| } | |
| /* Add padding to body to account for fixed footer */ | |
| body { | |
| padding-bottom: 60px; | |
| } | |
| /* Style for conversation controls */ | |
| .conversation-controls { | |
| margin: 15px 0; | |
| padding: 10px; | |
| background: rgba(74, 144, 226, 0.1); | |
| border-radius: 10px; | |
| border: 1px solid rgba(74, 144, 226, 0.2); | |
| } | |
| """ | |
| conversation_history = [] | |
| current_topic = "" | |
| def update_topics(language): | |
| return gr.Dropdown(choices=ai_friends.topics[language], value=ai_friends.topics[language][0]) | |
| def start_conversation(selected_topic, custom_topic): | |
| nonlocal conversation_history, current_topic | |
| topic = custom_topic.strip() if custom_topic.strip() else selected_topic | |
| if not topic: | |
| return "Please select or enter a topic!", "", conversation_history | |
| current_topic = topic | |
| conversation_history = [] | |
| return f"**Topic:** {topic}", format_conversation(conversation_history), conversation_history | |
| def continue_conversation(language): | |
| nonlocal conversation_history, current_topic | |
| if not current_topic: | |
| return format_conversation(conversation_history), conversation_history | |
| character_order = ["Alex", "Blake", "Charlie"] | |
| next_speaker = character_order[len(conversation_history) % 3] | |
| # Simulate thinking time | |
| time.sleep(1) | |
| response = ai_friends.get_demo_response(next_speaker, current_topic, language) | |
| conversation_history.append([next_speaker, response]) | |
| return format_conversation(conversation_history), conversation_history | |
| def add_user_message(message, language): | |
| nonlocal conversation_history | |
| if not message.strip(): | |
| return format_conversation(conversation_history), conversation_history, "" | |
| user_names = {"en": "You", "vi": "Bạn", "de": "Du"} | |
| user_name = user_names.get(language, "You") | |
| conversation_history.append([user_name, message.strip()]) | |
| return format_conversation(conversation_history), conversation_history, "" | |
| def clear_conversation(): | |
| nonlocal conversation_history, current_topic | |
| conversation_history = [] | |
| current_topic = "" | |
| return "", format_conversation(conversation_history), conversation_history | |
| def format_conversation(conversation): | |
| if not conversation: | |
| return "Select a topic and start the conversation to see AI friends debate!" | |
| html = "" | |
| for speaker, message in conversation: | |
| if speaker == "Alex": | |
| html += f"<div class='message-alex'><strong>🎭 Alex:</strong> {message}</div>" | |
| elif speaker == "Blake": | |
| html += f"<div class='message-blake'><strong>🌟 Blake:</strong> {message}</div>" | |
| elif speaker == "Charlie": | |
| html += f"<div class='message-charlie'><strong>🧠 Charlie:</strong> {message}</div>" | |
| else: | |
| html += f"<div class='message-user'><strong>👤 {speaker}:</strong> {message}</div>" | |
| return html | |
| with gr.Blocks(css=css, title="AI Friends Talk - Demo") as interface: | |
| gr.HTML(""" | |
| <div class="demo-banner"> | |
| 🎭 DEMO MODE - Using simulated AI responses (no API keys required) | |
| </div> | |
| """) | |
| gr.HTML(""" | |
| <div style="text-align: center; background: linear-gradient(135deg, #4A90E2 0%, #FF6B9D 100%); color: white; padding: 20px; border-radius: 10px; margin-bottom: 20px;"> | |
| <h1>🤖 AI Friends Talk - Demo</h1> | |
| <p>Watch AI friends debate fun topics! (Demo version with simulated responses)</p> | |
| <div style="margin-top: 10px;">🧠 <strong>Digitized Brains</strong></div> | |
| </div> | |
| """) | |
| gr.HTML(""" | |
| <div style="display: flex; justify-content: center; gap: 15px; margin: 20px 0;"> | |
| <div style="background: linear-gradient(135deg, #FF6B6B 0%, #FF8E53 100%); padding: 15px; border-radius: 10px; color: white; text-align: center;"> | |
| <h4>🎭 Alex</h4> | |
| <p style="margin: 0; font-size: 12px;">Witty debater (Demo)</p> | |
| </div> | |
| <div style="background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%); padding: 15px; border-radius: 10px; color: white; text-align: center;"> | |
| <h4>🌟 Blake</h4> | |
| <p style="margin: 0; font-size: 12px;">Creative optimist (Demo)</p> | |
| </div> | |
| <div style="background: linear-gradient(135deg, #45B7D1 0%, #96C93D 100%); padding: 15px; border-radius: 10px; color: white; text-align: center;"> | |
| <h4>🧠 Charlie</h4> | |
| <p style="margin: 0; font-size: 12px;">Logical analyst (Demo)</p> | |
| </div> | |
| </div> | |
| """) | |
| with gr.Row(): | |
| language = gr.Dropdown( | |
| choices=[("🇺🇸 English", "en"), ("🇻🇳 Tiếng Việt", "vi"), ("🇩🇪 Deutsch", "de")], | |
| value="en", | |
| label="Language" | |
| ) | |
| gr.Markdown("### Topic Selection") | |
| with gr.Row(): | |
| topic_dropdown = gr.Dropdown( | |
| choices=ai_friends.topics["en"], | |
| value=ai_friends.topics["en"][0], | |
| label="Predefined Topics", | |
| scale=2 | |
| ) | |
| custom_topic = gr.Textbox( | |
| placeholder="Enter your custom topic here...", | |
| label="Custom Topic", | |
| scale=2 | |
| ) | |
| start_btn = gr.Button("🎬 Start Conversation", variant="primary", size="lg") | |
| topic_display = gr.Markdown("") | |
| gr.Markdown("### Conversation") | |
| conversation_display = gr.HTML("") | |
| # Conversation controls moved below conversation | |
| gr.HTML("<div class='conversation-controls'>") | |
| with gr.Row(): | |
| continue_btn = gr.Button("▶️ Continue", variant="secondary", scale=1) | |
| clear_btn = gr.Button("🔄 Clear", variant="stop", scale=1) | |
| gr.HTML("</div>") | |
| gr.Markdown("### Add Your Message") | |
| with gr.Row(): | |
| user_message = gr.Textbox( | |
| placeholder="Type your message to join the conversation...", | |
| label="Your Message", | |
| scale=4 | |
| ) | |
| send_btn = gr.Button("📤 Send", scale=1) | |
| conversation_state = gr.State([]) | |
| # Event handlers | |
| language.change( | |
| update_topics, | |
| inputs=[language], | |
| outputs=[topic_dropdown] | |
| ) | |
| start_btn.click( | |
| start_conversation, | |
| inputs=[topic_dropdown, custom_topic], | |
| outputs=[topic_display, conversation_display, conversation_state] | |
| ) | |
| continue_btn.click( | |
| continue_conversation, | |
| inputs=[language], | |
| outputs=[conversation_display, conversation_state] | |
| ) | |
| send_btn.click( | |
| add_user_message, | |
| inputs=[user_message, language], | |
| outputs=[conversation_display, conversation_state, user_message] | |
| ) | |
| clear_btn.click( | |
| clear_conversation, | |
| outputs=[topic_display, conversation_display, conversation_state] | |
| ) | |
| # Custom footer to cover Gradio attribution | |
| gr.HTML(""" | |
| <div class="custom-footer"> | |
| <div style="display: flex; justify-content: center; align-items: center; gap: 15px;"> | |
| <div style="display: flex; align-items: center; gap: 8px;"> | |
| <div style="background: rgba(255,255,255,0.2); padding: 8px 15px; border-radius: 20px; font-size: 16px;"> | |
| 🧠 DB | |
| </div> | |
| <span style="font-size: 18px; font-weight: bold;">Digitized Brains</span> | |
| </div> | |
| <div style="font-size: 14px; opacity: 0.9;"> | |
| AI Friends Talk Demo - Experience the Future of AI Conversation | |
| </div> | |
| </div> | |
| </div> | |
| """) | |
| return interface | |
| if __name__ == "__main__": | |
| try: | |
| print("AI Friends Talk Demo - Starting...") | |
| print("This is a demo version with simulated AI responses") | |
| print("No API keys required!") | |
| print("Interface will be available at: http://localhost:7860") | |
| interface = create_demo_interface() | |
| interface.launch( | |
| share=True, | |
| debug=True, | |
| server_port=7860, | |
| show_error=True | |
| ) | |
| except KeyboardInterrupt: | |
| print("\nDemo stopped by user") | |
| except Exception as e: | |
| print(f"Error starting demo: {str(e)}") | |
| import traceback | |
| traceback.print_exc() |