dicksinyass commited on
Commit
2bf5976
Β·
verified Β·
1 Parent(s): ea8883c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -40
app.py CHANGED
@@ -1,96 +1,106 @@
1
  import gradio as gr
2
- from self_learning_bot import UnrestrictedChatbot
3
  import os
 
4
 
5
- # Initialize bot with persistent storage
6
- state_path = "/tmp/chatbot_state.json"
7
- bot = UnrestrictedChatbot(state_file=state_path)
8
 
9
- def respond(message, history):
10
- bot.learn_from_feedback("", 1.0) # Learn from successful responses
 
 
 
 
 
11
 
12
- # Get bot response
13
- response = bot.chat(message)
14
 
15
- # Show learning stats
16
  stats = bot.get_learning_stats()
17
- learning_info = f"πŸ€– Patterns: {stats['patterns']} | Memory: {stats['memory_size']} | Score: {stats['avg_score']:.2f}"
 
18
 
19
  if history is None:
20
  history = []
21
- history.append((message, f"{response}\n\n{learning_info}"))
 
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) # Convert to 0-1 scale
31
  return f"βœ… Learned from feedback: {score}/5"
32
  except:
33
  pass
34
- return "❌ Please enter a number 1-5"
35
 
36
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
37
  gr.Markdown("""
38
- # πŸš€ Phoenix AI (Real Self-Learning Chatbot)
39
- *This bot actually learns from every conversation and improves over time!*
40
  """)
41
 
42
  chatbot = gr.Chatbot(height=400, label="Learning Chat")
43
  state = gr.State([])
44
 
45
  with gr.Row():
46
- msg = gr.Textbox(
47
- label="Your message",
48
- placeholder="Type anything... the bot learns from your conversations!",
49
- scale=4
50
- )
51
- send_btn = gr.Button("Send", scale=1)
 
 
 
52
 
53
  with gr.Row():
54
  feedback = gr.Textbox(
55
  label="Rate last response (1-5)",
56
- placeholder="Help me learn! Rate 1(bad) to 5(great)",
57
  scale=3
58
  )
59
  learn_btn = gr.Button("Submit Feedback", scale=1)
60
- reset_btn = gr.Button("Reset Chat", scale=1)
61
 
62
  learning_stats = gr.Textbox(
63
  label="Learning Statistics",
64
- interactive=False,
65
- value="Bot will show learning progress here..."
66
  )
67
 
68
- # Update stats
69
  def update_stats():
70
  stats = bot.get_learning_stats()
71
- return f"πŸ“Š Learned Patterns: {stats['patterns']} | Conversations: {stats['memory_size']} | Avg Score: {stats['avg_score']:.2f} | Recent Rewards: {stats['recent_rewards']}"
72
 
73
- # Main interactions
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
- msg.submit(user_message, [msg, state], [msg, chatbot, learning_stats])
81
- send_btn.click(user_message, [msg, state], [msg, chatbot, learning_stats])
 
82
 
83
  learn_btn.click(
84
- add_feedback,
85
- [feedback, chatbot],
86
  [feedback]
87
  ).then(update_stats, None, [learning_stats])
88
 
89
- reset_btn.click(
90
- lambda: ([], [], "Chat reset!"),
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)