Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from free_web_bot import FreeWebBot | |
| # Initialize the bot | |
| bot = FreeWebBot() | |
| def respond(message, history, enable_search): | |
| """Process user message and return response""" | |
| if not message or not message.strip(): | |
| return history, history | |
| # Get response from bot | |
| response, search_used, sources = bot.chat(message, use_web_search=enable_search) | |
| # Create learning info display | |
| stats = bot.get_learning_stats() | |
| if search_used and sources: | |
| search_info = f"π Web Search ({len(sources)} sources)" | |
| elif search_used: | |
| search_info = "π Search Attempted" | |
| else: | |
| search_info = "π From Knowledge" | |
| learning_info = f"{search_info} | Patterns: {stats['patterns']} | Score: {stats['avg_score']:.2f}" | |
| # Format full response | |
| full_response = f"{response}\n\n{learning_info}" | |
| # Update history | |
| if history is None: | |
| history = [] | |
| history.append((message, full_response)) | |
| return history, history | |
| def add_feedback(feedback_text, history): | |
| """Process user feedback""" | |
| if not feedback_text or not history: | |
| return "Please enter feedback and ensure there's a conversation" | |
| try: | |
| score = float(feedback_text) | |
| if 1 <= score <= 5: | |
| # Get the last user message from history | |
| last_user_message = history[-1][0] if history else "" | |
| bot.learn_from_feedback(last_user_message, score / 5.0) | |
| return f"β Learned from feedback: {score}/5" | |
| else: | |
| return "β Please enter a number between 1-5" | |
| except ValueError: | |
| return "β Please enter a valid number 1-5" | |
| def clear_chat(): | |
| """Clear the chat history""" | |
| return [], [], "Chat cleared!" | |
| # Create the Gradio interface | |
| with gr.Blocks(theme=gr.themes.Soft(), title="Phoenix AI - Free Web Chat") as demo: | |
| gr.Markdown(""" | |
| # π Phoenix AI - Free Web Chatbot | |
| **Completely free chatbot with real web search capabilities!** | |
| *Try asking about: current news, weather, sports, or any topic you're curious about!* | |
| """) | |
| # Chat interface | |
| chatbot = gr.Chatbot( | |
| label="Conversation", | |
| height=500, | |
| show_copy_button=True | |
| ) | |
| # State to store conversation history | |
| state = gr.State(value=[]) | |
| # Input section | |
| with gr.Row(): | |
| message_input = gr.Textbox( | |
| label="Your message", | |
| placeholder="Ask me anything and I'll search the web for answers...", | |
| scale=4, | |
| container=False | |
| ) | |
| with gr.Column(scale=1): | |
| search_checkbox = gr.Checkbox( | |
| label="Enable Web Search", | |
| value=True, | |
| info="Search web for current information" | |
| ) | |
| send_button = gr.Button("Send", variant="primary") | |
| # Feedback section | |
| with gr.Row(): | |
| feedback_input = gr.Textbox( | |
| label="Rate the last response (1-5)", | |
| placeholder="Help me learn by rating responses 1 (poor) to 5 (excellent)", | |
| scale=3 | |
| ) | |
| feedback_button = gr.Button("Submit Rating", scale=1) | |
| clear_button = gr.Button("Clear Chat", scale=1) | |
| # Learning statistics | |
| stats_display = gr.Textbox( | |
| label="Learning Progress", | |
| value="Ready to chat and learn!", | |
| interactive=False, | |
| max_lines=2 | |
| ) | |
| # Update statistics display | |
| def update_statistics(): | |
| stats = bot.get_learning_stats() | |
| return f"π Learned: {stats['patterns']} patterns | Memory: {stats['memory_size']} conversations | Success Rate: {stats['avg_score']:.2f}/1.0" | |
| # Handle message submission | |
| def handle_message(user_message, chat_history, search_enabled): | |
| if not user_message or not user_message.strip(): | |
| return "", chat_history, update_statistics() | |
| new_history = respond(user_message, chat_history, search_enabled) | |
| return "", new_history[0], update_statistics() | |
| # Event handlers | |
| message_input.submit( | |
| handle_message, | |
| inputs=[message_input, state, search_checkbox], | |
| outputs=[message_input, chatbot, stats_display] | |
| ) | |
| send_button.click( | |
| handle_message, | |
| inputs=[message_input, state, search_checkbox], | |
| outputs=[message_input, chatbot, stats_display] | |
| ) | |
| feedback_button.click( | |
| add_feedback, | |
| inputs=[feedback_input, chatbot], | |
| outputs=[feedback_input] | |
| ).then( | |
| update_statistics, | |
| outputs=[stats_display] | |
| ) | |
| clear_button.click( | |
| clear_chat, | |
| outputs=[chatbot, state, stats_display] | |
| ) | |
| # Launch the application | |
| if __name__ == "__main__": | |
| demo.launch( | |
| share=False, | |
| show_error=True, | |
| debug=True | |
| ) |