Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,157 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
# Initialize the
|
| 6 |
-
bot =
|
| 7 |
|
| 8 |
def respond(message, history, enable_search):
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
stats = bot.get_learning_stats()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
learning_info = f"{search_info} | Patterns: {stats['patterns']} | Score: {stats['avg_score']:.2f}"
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if history is None:
|
| 18 |
history = []
|
| 19 |
-
|
| 20 |
-
full_response = f"{response}\n\n{learning_info}"
|
| 21 |
history.append((message, full_response))
|
|
|
|
| 22 |
return history, history
|
| 23 |
|
| 24 |
-
def add_feedback(
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
gr.Markdown("""
|
| 37 |
-
# π Phoenix AI -
|
| 38 |
-
**
|
| 39 |
-
|
|
|
|
| 40 |
""")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|
|
| 45 |
with gr.Row():
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
with gr.Column(scale=1):
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
|
|
|
| 56 |
with gr.Row():
|
| 57 |
-
|
| 58 |
-
label="Rate
|
| 59 |
-
placeholder="Help me learn
|
| 60 |
scale=3
|
| 61 |
)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# Update
|
| 72 |
-
def
|
| 73 |
stats = bot.get_learning_stats()
|
| 74 |
-
return f"π Learned: {stats['patterns']} patterns | Memory: {stats['memory_size']} |
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
def user_message
|
| 78 |
-
if not
|
| 79 |
-
return "",
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
# Event handlers
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
add_feedback,
|
| 89 |
-
[
|
| 90 |
-
[
|
| 91 |
-
).then(
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
)
|
| 98 |
|
|
|
|
| 99 |
if __name__ == "__main__":
|
| 100 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from free_web_bot import FreeWebBot
|
|
|
|
| 3 |
|
| 4 |
+
# Initialize the bot
|
| 5 |
+
bot = FreeWebBot()
|
| 6 |
|
| 7 |
def respond(message, history, enable_search):
|
| 8 |
+
"""Process user message and return response"""
|
| 9 |
+
if not message or not message.strip():
|
| 10 |
+
return history, history
|
| 11 |
|
| 12 |
+
# Get response from bot
|
| 13 |
+
response, search_used, sources = bot.chat(message, use_web_search=enable_search)
|
| 14 |
+
|
| 15 |
+
# Create learning info display
|
| 16 |
stats = bot.get_learning_stats()
|
| 17 |
+
if search_used and sources:
|
| 18 |
+
search_info = f"π Web Search ({len(sources)} sources)"
|
| 19 |
+
elif search_used:
|
| 20 |
+
search_info = "π Search Attempted"
|
| 21 |
+
else:
|
| 22 |
+
search_info = "π From Knowledge"
|
| 23 |
+
|
| 24 |
learning_info = f"{search_info} | Patterns: {stats['patterns']} | Score: {stats['avg_score']:.2f}"
|
| 25 |
|
| 26 |
+
# Format full response
|
| 27 |
+
full_response = f"{response}\n\n{learning_info}"
|
| 28 |
+
|
| 29 |
+
# Update history
|
| 30 |
if history is None:
|
| 31 |
history = []
|
|
|
|
|
|
|
| 32 |
history.append((message, full_response))
|
| 33 |
+
|
| 34 |
return history, history
|
| 35 |
|
| 36 |
+
def add_feedback(feedback_text, history):
|
| 37 |
+
"""Process user feedback"""
|
| 38 |
+
if not feedback_text or not history:
|
| 39 |
+
return "Please enter feedback and ensure there's a conversation"
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
score = float(feedback_text)
|
| 43 |
+
if 1 <= score <= 5:
|
| 44 |
+
# Get the last user message from history
|
| 45 |
+
last_user_message = history[-1][0] if history else ""
|
| 46 |
+
bot.learn_from_feedback(last_user_message, score / 5.0)
|
| 47 |
+
return f"β
Learned from feedback: {score}/5"
|
| 48 |
+
else:
|
| 49 |
+
return "β Please enter a number between 1-5"
|
| 50 |
+
except ValueError:
|
| 51 |
+
return "β Please enter a valid number 1-5"
|
| 52 |
|
| 53 |
+
def clear_chat():
|
| 54 |
+
"""Clear the chat history"""
|
| 55 |
+
return [], [], "Chat cleared!"
|
| 56 |
+
|
| 57 |
+
# Create the Gradio interface
|
| 58 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Phoenix AI - Free Web Chat") as demo:
|
| 59 |
gr.Markdown("""
|
| 60 |
+
# π Phoenix AI - Free Web Chatbot
|
| 61 |
+
**Completely free chatbot with real web search capabilities!**
|
| 62 |
+
|
| 63 |
+
*Try asking about: current news, weather, sports, or any topic you're curious about!*
|
| 64 |
""")
|
| 65 |
|
| 66 |
+
# Chat interface
|
| 67 |
+
chatbot = gr.Chatbot(
|
| 68 |
+
label="Conversation",
|
| 69 |
+
height=500,
|
| 70 |
+
show_copy_button=True
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# State to store conversation history
|
| 74 |
+
state = gr.State(value=[])
|
| 75 |
|
| 76 |
+
# Input section
|
| 77 |
with gr.Row():
|
| 78 |
+
message_input = gr.Textbox(
|
| 79 |
+
label="Your message",
|
| 80 |
+
placeholder="Ask me anything and I'll search the web for answers...",
|
| 81 |
+
scale=4,
|
| 82 |
+
container=False
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
with gr.Column(scale=1):
|
| 86 |
+
search_checkbox = gr.Checkbox(
|
| 87 |
+
label="Enable Web Search",
|
| 88 |
+
value=True,
|
| 89 |
+
info="Search web for current information"
|
| 90 |
+
)
|
| 91 |
+
send_button = gr.Button("Send", variant="primary")
|
| 92 |
|
| 93 |
+
# Feedback section
|
| 94 |
with gr.Row():
|
| 95 |
+
feedback_input = gr.Textbox(
|
| 96 |
+
label="Rate the last response (1-5)",
|
| 97 |
+
placeholder="Help me learn by rating responses 1 (poor) to 5 (excellent)",
|
| 98 |
scale=3
|
| 99 |
)
|
| 100 |
+
feedback_button = gr.Button("Submit Rating", scale=1)
|
| 101 |
+
clear_button = gr.Button("Clear Chat", scale=1)
|
| 102 |
|
| 103 |
+
# Learning statistics
|
| 104 |
+
stats_display = gr.Textbox(
|
| 105 |
+
label="Learning Progress",
|
| 106 |
+
value="Ready to chat and learn!",
|
| 107 |
+
interactive=False,
|
| 108 |
+
max_lines=2
|
| 109 |
)
|
| 110 |
|
| 111 |
+
# Update statistics display
|
| 112 |
+
def update_statistics():
|
| 113 |
stats = bot.get_learning_stats()
|
| 114 |
+
return f"π Learned: {stats['patterns']} patterns | Memory: {stats['memory_size']} conversations | Success Rate: {stats['avg_score']:.2f}/1.0"
|
| 115 |
|
| 116 |
+
# Handle message submission
|
| 117 |
+
def handle_message(user_message, chat_history, search_enabled):
|
| 118 |
+
if not user_message or not user_message.strip():
|
| 119 |
+
return "", chat_history, update_statistics()
|
| 120 |
+
|
| 121 |
+
new_history = respond(user_message, chat_history, search_enabled)
|
| 122 |
+
return "", new_history[0], update_statistics()
|
| 123 |
|
| 124 |
# Event handlers
|
| 125 |
+
message_input.submit(
|
| 126 |
+
handle_message,
|
| 127 |
+
inputs=[message_input, state, search_checkbox],
|
| 128 |
+
outputs=[message_input, chatbot, stats_display]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
send_button.click(
|
| 132 |
+
handle_message,
|
| 133 |
+
inputs=[message_input, state, search_checkbox],
|
| 134 |
+
outputs=[message_input, chatbot, stats_display]
|
| 135 |
+
)
|
| 136 |
|
| 137 |
+
feedback_button.click(
|
| 138 |
add_feedback,
|
| 139 |
+
inputs=[feedback_input, chatbot],
|
| 140 |
+
outputs=[feedback_input]
|
| 141 |
+
).then(
|
| 142 |
+
update_statistics,
|
| 143 |
+
outputs=[stats_display]
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
clear_button.click(
|
| 147 |
+
clear_chat,
|
| 148 |
+
outputs=[chatbot, state, stats_display]
|
| 149 |
)
|
| 150 |
|
| 151 |
+
# Launch the application
|
| 152 |
if __name__ == "__main__":
|
| 153 |
+
demo.launch(
|
| 154 |
+
share=False,
|
| 155 |
+
show_error=True,
|
| 156 |
+
debug=True
|
| 157 |
+
)
|