Tafar commited on
Commit
6cdad6c
Β·
1 Parent(s): 7978b3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -38
app.py CHANGED
@@ -1,10 +1,31 @@
1
- from chatbot_ui import run_chat_ui
2
  import gradio as gr
3
- import random
4
- import time
5
 
6
  topics = ["finance", "music", "sports", "technology", "health care"]
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  with gr.Blocks() as demo:
9
  chatbot = gr.Chatbot()
10
  msg = gr.Textbox()
@@ -18,37 +39,26 @@ with gr.Blocks() as demo:
18
 
19
  def user(user_message):
20
  # Extract topic from the user's message
21
- topic = None
22
- for t in topics:
23
- if t in user_message.lower():
24
- topic = t
25
- break
26
 
 
 
 
 
 
27
  # Respond based on the detected topic
28
- if topic == "finance":
29
- return "Sure, let's talk about finance!", [user_message, None]
30
- elif topic == "music":
31
- return "Music is wonderful! What's your favorite artist or band?", [user_message, None]
32
- elif topic == "sports":
33
- return "Sports are exciting! What's your favorite sport?", [user_message, None]
34
- elif topic == "technology":
35
- return "Technology is fascinating! Any specific tech topic you're interested in?", [user_message, None]
36
- elif topic == "health care":
37
- return "Health is important! What health-related topic would you like to discuss?", [user_message, None]
38
  else:
39
  return "I'm not sure which topic you're referring to. Please specify one of: " + ", ".join(topics), [user_message, None]
40
 
41
  def bot(user_message):
42
  # Check the last user message for the detected topic
43
  last_user_message = history[-1][0].lower() if history else ""
44
- topic = None
45
- for t in topics:
46
- if t in last_user_message:
47
- topic = t
48
- break
49
 
50
  # Respond based on the detected topic or provide a generic response
51
- if topic is not None:
52
  response = history[-1][1]
53
  else:
54
  response = "That's interesting! What else would you like to talk about?"
@@ -56,23 +66,11 @@ with gr.Blocks() as demo:
56
  history[-1][1] = response
57
  return response
58
 
59
- def process_feedback(feedback_value):
60
- if feedback_value:
61
- feedback_index = {"πŸ‘": 1, "πŸ‘Ž": -1}[feedback_value]
62
- last_user_message, last_bot_response = history[-1]
63
- # Use the feedback to update the bot's response in a simple way
64
- history[-1][1] = f"{last_bot_response} (Feedback: {feedback_index})"
65
- print(f"Feedback received: {feedback_value} for: {last_user_message} - {last_bot_response}")
66
-
67
  msg.submit(user, [msg], [chatbot]).then(
68
  lambda user_response: bot(user_response[0]), chatbot
69
  )
70
- feedback.change(process_feedback, queue=True)
71
  clear.click(lambda: None, None, chatbot, queue=False)
72
 
73
-
74
  if __name__ == "__main__":
75
- run_chat_ui()
76
-
77
- demo.queue()
78
- demo.launch(share=True)
 
 
1
  import gradio as gr
 
 
2
 
3
  topics = ["finance", "music", "sports", "technology", "health care"]
4
 
5
+ def generate_ai_blog():
6
+ return (
7
+ "**How Artificial Intelligence Impacts in Everyday Lives and Industries**\n\n"
8
+ "## Introduction:\n\n"
9
+ "Artificial Intelligence (AI) has emerged as a transformative force, reshaping various aspects of our daily lives and industries. "
10
+ "From personalized recommendations on streaming platforms to advanced automation in manufacturing, AI has become an integral part of our interconnected world.\n\n"
11
+ "## Subheading 1: Enhancing Daily Convenience:\n\n"
12
+ "In the realm of everyday life, AI manifests in applications that enhance convenience. Virtual assistants, like Siri and Alexa, leverage AI to understand and respond to natural language, providing hands-free assistance for tasks ranging from setting reminders to answering queries. Smart home devices utilize AI algorithms to learn user preferences, adjusting climate control, and lighting to optimize comfort.\n\n"
13
+ "## Subheading 2: Revolutionizing Industries:\n\n"
14
+ "In industries, the impact of AI is profound, ushering in a new era of efficiency and innovation. Automation powered by AI streamlines manufacturing processes, reducing errors and increasing productivity. In healthcare, AI algorithms analyze vast datasets to aid in diagnostics and treatment planning, fostering more accurate and personalized patient care.\n\n"
15
+ "## Subheading 3: Transforming Decision-Making:\n\n"
16
+ "AI's analytical capabilities are revolutionizing decision-making across sectors. Businesses leverage AI-driven analytics to gain insights from large datasets, informing strategic decisions and predicting market trends. In finance, algorithms analyze market data in real-time, optimizing investment strategies and risk management. The predictive power of AI is transforming decision landscapes, empowering industries to adapt and thrive in a dynamic environment.\n\n"
17
+ "## Conclusion:\n\n"
18
+ "As we navigate the evolving landscape of technology, the impact of AI on everyday lives and industries is undeniable. From simplifying daily tasks to redefining industrial processes, artificial intelligence continues to shape the way we live and work. Embracing the potential of AI opens doors to unprecedented possibilities, promising a future where innovation and efficiency coexist to enrich our lives and drive progress."
19
+ )
20
+
21
+ def process_feedback(feedback_value, history):
22
+ if feedback_value:
23
+ feedback_index = {"πŸ‘": 1, "πŸ‘Ž": -1}[feedback_value]
24
+ last_user_message, last_bot_response = history[-1]
25
+ # Use the feedback to update the bot's response in a simple way
26
+ history[-1][1] = f"{last_bot_response} (Feedback: {feedback_index})"
27
+ print(f"Feedback received: {feedback_value} for: {last_user_message} - {last_bot_response}")
28
+
29
  with gr.Blocks() as demo:
30
  chatbot = gr.Chatbot()
31
  msg = gr.Textbox()
 
39
 
40
  def user(user_message):
41
  # Extract topic from the user's message
42
+ topic = next((t for t in topics if t in user_message.lower()), None)
 
 
 
 
43
 
44
+ # Check if the user is asking for a blog
45
+ if "blog" in user_message.lower():
46
+ blog_text = generate_ai_blog()
47
+ # Append the blog output to the chat history
48
+ return blog_text, [user_message, blog_text]
49
  # Respond based on the detected topic
50
+ elif topic:
51
+ return f"Sure, let's talk about {topic}!", [user_message, None]
 
 
 
 
 
 
 
 
52
  else:
53
  return "I'm not sure which topic you're referring to. Please specify one of: " + ", ".join(topics), [user_message, None]
54
 
55
  def bot(user_message):
56
  # Check the last user message for the detected topic
57
  last_user_message = history[-1][0].lower() if history else ""
58
+ topic = next((t for t in topics if t in last_user_message), None)
 
 
 
 
59
 
60
  # Respond based on the detected topic or provide a generic response
61
+ if topic:
62
  response = history[-1][1]
63
  else:
64
  response = "That's interesting! What else would you like to talk about?"
 
66
  history[-1][1] = response
67
  return response
68
 
 
 
 
 
 
 
 
 
69
  msg.submit(user, [msg], [chatbot]).then(
70
  lambda user_response: bot(user_response[0]), chatbot
71
  )
72
+ feedback.change(lambda feedback_value: process_feedback(feedback_value, history), queue=True)
73
  clear.click(lambda: None, None, chatbot, queue=False)
74
 
 
75
  if __name__ == "__main__":
76
+ gr.Interface([demo], "chatbot").launch()