dicksinyass commited on
Commit
14ad231
Β·
verified Β·
1 Parent(s): dea9a55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -64
app.py CHANGED
@@ -1,100 +1,157 @@
1
  import gradio as gr
2
- from web_learning_bot import WebEnhancedBot
3
- import os
4
 
5
- # Initialize the web-enhanced bot
6
- bot = WebEnhancedBot()
7
 
8
  def respond(message, history, enable_search):
9
- # Get response with actual web integration
10
- response, search_used = bot.chat_with_web_search(message, use_search=enable_search)
 
11
 
12
- # Display context
13
- search_info = "πŸ” **Web Search Used**" if search_used else "πŸ’­ **From Knowledge**"
 
 
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(feedback, history):
25
- if feedback and history:
26
- try:
27
- score = float(feedback)
28
- if 1 <= score <= 5:
29
- bot.learn_from_feedback(history[-1][0], score / 5.0)
30
- return f"βœ… Learned from feedback: {score}/5"
31
- except:
32
- pass
33
- return "❌ Please enter 1-5"
 
 
 
 
 
 
34
 
35
- with gr.Blocks(theme=gr.themes.Soft(), title="Phoenix AI - Real Web Search") as demo:
 
 
 
 
 
36
  gr.Markdown("""
37
- # 🌐 Phoenix AI - Real Web Search
38
- **Now actually answers questions using real internet information!**
39
- *Try: "current news", "weather in Tokyo", "latest AI developments", etc.*
 
40
  """)
41
 
42
- chatbot = gr.Chatbot(height=400, label="Web-Enhanced Chat")
43
- state = gr.State([])
 
 
 
 
 
 
 
44
 
 
45
  with gr.Row():
46
- with gr.Column(scale=4):
47
- msg = gr.Textbox(
48
- label="Ask me anything",
49
- placeholder="I'll search the web and give you real answers! Try: 'current news' or 'weather in London'",
50
- container=False
51
- )
 
52
  with gr.Column(scale=1):
53
- search_toggle = gr.Checkbox(label="πŸ” Enable Web Search", value=True)
54
- send_btn = gr.Button("Send", variant="primary")
 
 
 
 
55
 
 
56
  with gr.Row():
57
- feedback = gr.Textbox(
58
- label="Rate this response (1-5)",
59
- placeholder="Help me learn! Rate 1-5",
60
  scale=3
61
  )
62
- learn_btn = gr.Button("Submit Feedback", scale=1)
63
- clear_btn = gr.Button("Clear Chat", scale=1)
64
 
65
- learning_stats = gr.Textbox(
66
- label="Learning Statistics",
67
- value="Ready to search and answer questions...",
68
- interactive=False
 
 
69
  )
70
 
71
- # Update stats
72
- def update_stats():
73
  stats = bot.get_learning_stats()
74
- return f"πŸ“Š Learned: {stats['patterns']} patterns | Memory: {stats['memory_size']} | Recent Success: {stats['recent_rewards']}/10"
75
 
76
- # Main interaction
77
- def user_message(message, history, search_toggle):
78
- if not message.strip():
79
- return "", history
80
- new_history = respond(message, history or [], search_toggle)
81
- return "", new_history[0], update_stats()
 
82
 
83
  # Event handlers
84
- msg.submit(user_message, [msg, state, search_toggle], [msg, chatbot, learning_stats])
85
- send_btn.click(user_message, [msg, state, search_toggle], [msg, chatbot, learning_stats])
 
 
 
 
 
 
 
 
 
86
 
87
- learn_btn.click(
88
  add_feedback,
89
- [feedback, chatbot],
90
- [feedback]
91
- ).then(update_stats, None, [learning_stats])
92
-
93
- clear_btn.click(
94
- lambda: ([], [], "Chat cleared!"),
95
- None,
96
- [chatbot, state, learning_stats]
 
 
97
  )
98
 
 
99
  if __name__ == "__main__":
100
- demo.launch(share=True, show_error=True)
 
 
 
 
 
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
+ )