Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,106 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from self_learning_bot import
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
# Initialize
|
| 6 |
-
|
| 7 |
-
bot = UnrestrictedChatbot(state_file=state_path)
|
| 8 |
|
| 9 |
-
def respond(message, history):
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#
|
| 16 |
stats = bot.get_learning_stats()
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
if history is None:
|
| 20 |
history = []
|
| 21 |
-
|
|
|
|
| 22 |
return history, history
|
| 23 |
|
| 24 |
def add_feedback(feedback, history):
|
| 25 |
if feedback and history:
|
| 26 |
-
# Learn from user feedback (1-5 scale)
|
| 27 |
try:
|
| 28 |
score = float(feedback)
|
| 29 |
if 1 <= score <= 5:
|
| 30 |
-
bot.learn_from_feedback(history[-1][0], score / 5.0)
|
| 31 |
return f"β
Learned from feedback: {score}/5"
|
| 32 |
except:
|
| 33 |
pass
|
| 34 |
-
return "β Please enter
|
| 35 |
|
| 36 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 37 |
gr.Markdown("""
|
| 38 |
-
# π Phoenix AI
|
| 39 |
-
|
| 40 |
""")
|
| 41 |
|
| 42 |
chatbot = gr.Chatbot(height=400, label="Learning Chat")
|
| 43 |
state = gr.State([])
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
feedback = gr.Textbox(
|
| 55 |
label="Rate last response (1-5)",
|
| 56 |
-
placeholder="Help me learn! Rate 1
|
| 57 |
scale=3
|
| 58 |
)
|
| 59 |
learn_btn = gr.Button("Submit Feedback", scale=1)
|
| 60 |
-
|
| 61 |
|
| 62 |
learning_stats = gr.Textbox(
|
| 63 |
label="Learning Statistics",
|
| 64 |
-
|
| 65 |
-
|
| 66 |
)
|
| 67 |
|
| 68 |
-
# Update stats
|
| 69 |
def update_stats():
|
| 70 |
stats = bot.get_learning_stats()
|
| 71 |
-
return f"π Learned
|
| 72 |
|
| 73 |
-
# Main
|
| 74 |
-
def user_message(message, history):
|
| 75 |
if not message.strip():
|
| 76 |
return "", history
|
| 77 |
-
new_history = respond(message, history or [])
|
| 78 |
return "", new_history[0], update_stats()
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
learn_btn.click(
|
| 84 |
-
add_feedback,
|
| 85 |
-
[feedback, chatbot],
|
| 86 |
[feedback]
|
| 87 |
).then(update_stats, None, [learning_stats])
|
| 88 |
|
| 89 |
-
|
| 90 |
-
lambda: ([], [], "Chat
|
| 91 |
-
None,
|
| 92 |
[chatbot, state, learning_stats]
|
| 93 |
)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
-
demo.launch(show_error=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from self_learning_bot import EnhancedLearningBot
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
# Initialize the enhanced bot
|
| 7 |
+
bot = EnhancedLearningBot()
|
|
|
|
| 8 |
|
| 9 |
+
def respond(message, history, enable_search):
|
| 10 |
+
# Get response with optional web search
|
| 11 |
+
response, search_context = bot.chat(
|
| 12 |
+
message,
|
| 13 |
+
use_web_search=enable_search,
|
| 14 |
+
conversation_history=history
|
| 15 |
+
)
|
| 16 |
|
| 17 |
+
# Learn from this interaction
|
| 18 |
+
bot.learn_from_interaction(message, response, search_context)
|
| 19 |
|
| 20 |
+
# Display learning stats and search info
|
| 21 |
stats = bot.get_learning_stats()
|
| 22 |
+
search_info = "π Web Search Used" if search_context else "π From Memory"
|
| 23 |
+
learning_info = f"{search_info} | Patterns: {stats['patterns']} | Score: {stats['avg_score']:.2f}"
|
| 24 |
|
| 25 |
if history is None:
|
| 26 |
history = []
|
| 27 |
+
full_response = f"{response}\n\n{learning_info}"
|
| 28 |
+
history.append((message, full_response))
|
| 29 |
return history, history
|
| 30 |
|
| 31 |
def add_feedback(feedback, history):
|
| 32 |
if feedback and history:
|
|
|
|
| 33 |
try:
|
| 34 |
score = float(feedback)
|
| 35 |
if 1 <= score <= 5:
|
| 36 |
+
bot.learn_from_feedback(history[-1][0], score / 5.0)
|
| 37 |
return f"β
Learned from feedback: {score}/5"
|
| 38 |
except:
|
| 39 |
pass
|
| 40 |
+
return "β Please enter 1-5"
|
| 41 |
|
| 42 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Phoenix AI - Web Enhanced") as demo:
|
| 43 |
gr.Markdown("""
|
| 44 |
+
# π Phoenix AI - Web Enhanced
|
| 45 |
+
**Now with real-time web search and continuous learning!**
|
| 46 |
""")
|
| 47 |
|
| 48 |
chatbot = gr.Chatbot(height=400, label="Learning Chat")
|
| 49 |
state = gr.State([])
|
| 50 |
|
| 51 |
with gr.Row():
|
| 52 |
+
with gr.Column(scale=4):
|
| 53 |
+
msg = gr.Textbox(
|
| 54 |
+
label="Your message",
|
| 55 |
+
placeholder="Ask me anything... I can search the web for latest information!",
|
| 56 |
+
container=False
|
| 57 |
+
)
|
| 58 |
+
with gr.Column(scale=1):
|
| 59 |
+
search_toggle = gr.Checkbox(label="π Enable Web Search", value=True)
|
| 60 |
+
send_btn = gr.Button("Send", variant="primary")
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
feedback = gr.Textbox(
|
| 64 |
label="Rate last response (1-5)",
|
| 65 |
+
placeholder="Help me learn! Rate 1-5",
|
| 66 |
scale=3
|
| 67 |
)
|
| 68 |
learn_btn = gr.Button("Submit Feedback", scale=1)
|
| 69 |
+
clear_btn = gr.Button("Clear Chat", scale=1)
|
| 70 |
|
| 71 |
learning_stats = gr.Textbox(
|
| 72 |
label="Learning Statistics",
|
| 73 |
+
value="Ready to learn and search...",
|
| 74 |
+
interactive=False
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# Update stats display
|
| 78 |
def update_stats():
|
| 79 |
stats = bot.get_learning_stats()
|
| 80 |
+
return f"π Learned: {stats['patterns']} patterns | Memory: {stats['memory_size']} | Recent Success: {stats['recent_rewards']}/10"
|
| 81 |
|
| 82 |
+
# Main interaction flow
|
| 83 |
+
def user_message(message, history, search_toggle):
|
| 84 |
if not message.strip():
|
| 85 |
return "", history
|
| 86 |
+
new_history = respond(message, history or [], search_toggle)
|
| 87 |
return "", new_history[0], update_stats()
|
| 88 |
|
| 89 |
+
# Event handlers
|
| 90 |
+
msg.submit(user_message, [msg, state, search_toggle], [msg, chatbot, learning_stats])
|
| 91 |
+
send_btn.click(user_message, [msg, state, search_toggle], [msg, chatbot, learning_stats])
|
| 92 |
|
| 93 |
learn_btn.click(
|
| 94 |
+
add_feedback,
|
| 95 |
+
[feedback, chatbot],
|
| 96 |
[feedback]
|
| 97 |
).then(update_stats, None, [learning_stats])
|
| 98 |
|
| 99 |
+
clear_btn.click(
|
| 100 |
+
lambda: ([], [], "Chat cleared!"),
|
| 101 |
+
None,
|
| 102 |
[chatbot, state, learning_stats]
|
| 103 |
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
+
demo.launch(share=True, show_error=True)
|