KWK25 commited on
Commit
bc101f6
·
verified ·
1 Parent(s): 666b8ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -33
app.py CHANGED
@@ -1,15 +1,29 @@
1
  import gradio as gr
 
2
 
3
- # --- Normal Chatbot ---
4
- def respond(message, history):
5
- history = history + [(message, f"Echo: {message}")]
6
- return "", history
7
 
8
- # --- Quiz Bot ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  quiz_questions = [
10
- ("What is the capital of France?", "Paris"),
11
- ("2 + 2 = ?", "4"),
12
- ("What is the largest planet in our solar system?", "Jupiter")
13
  ]
14
 
15
  def quiz_bot(answer, state):
@@ -27,45 +41,54 @@ def quiz_bot(answer, state):
27
 
28
  return "", [(question, feedback)], (q_index, score)
29
 
30
- with gr.Blocks() as demo:
31
- # --- Tab 1: Normal Chatbot ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  with gr.Tab("Chat"):
33
- chatbot = gr.Chatbot()
 
34
  msg = gr.Textbox(label="Your message")
35
- clear = gr.Button("Clear Chat")
 
36
 
37
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
38
- clear.click(lambda: None, None, chatbot, queue=False)
39
-
40
- # --- Tab 2: Quiz Bot ---
41
  with gr.Tab("Quiz"):
42
  quiz_chat = gr.Chatbot()
43
  quiz_input = gr.Textbox(label="Your answer")
44
  start_button = gr.Button("Start Quiz")
 
45
 
46
- quiz_state = gr.State((0, 0)) # (question_index, score)
47
-
48
- # Start quiz with first question
49
  start_button.click(
50
  lambda: [("", [(quiz_questions[0][0], "Type your answer below")]), (0, 0)],
51
  None,
52
  [quiz_input, quiz_chat, quiz_state]
53
  )
54
-
55
  quiz_input.submit(quiz_bot, [quiz_input, quiz_state], [quiz_input, quiz_chat, quiz_state])
56
 
57
- # --- Tab 3: Information Section ---
58
  with gr.Tab("Information"):
59
- gr.Markdown("""
60
- ## ℹ️ Information
61
- Welcome to the chatbot app!
62
- - **Chat** tab: Talk to the bot freely.
63
- - **Quiz** tab: Test your knowledge with fun questions.
64
- - **Information** tab: Read instructions, rules, or any helpful content here.
65
-
66
- **Tip:** You can customize this section with HTML, Markdown, or even images.
67
- """)
68
- gr.Image("https://huggingface.co/front/assets/huggingface_logo-noborder.svg", label="Example Image")
69
-
70
- demo.launch()
71
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
 
4
+ # --- MAIN CHATBOT (Kpop) ---
5
+ client = InferenceClient("microsoft/phi-4")
 
 
6
 
7
+ def respond(message, history):
8
+ messages = [{"role": "system", "content": "You are a friendly kpop expert chatbot."}]
9
+
10
+ if history:
11
+ messages.extend(history)
12
+
13
+ messages.append({"role": "user", "content": message})
14
+
15
+ response = client.chat_completion(
16
+ messages,
17
+ max_tokens=1000
18
+ )
19
+
20
+ return "", history + [(message, response['choices'][0]['message']['content'].strip())]
21
+
22
+ # --- QUIZ BOT ---
23
  quiz_questions = [
24
+ ("What is the capital of South Korea?", "Seoul"),
25
+ ("Which K-pop group sings 'Dynamite'?", "BTS"),
26
+ ("Who is known as the 'Nation's Girl Group' in Korea?", "Girls' Generation")
27
  ]
28
 
29
  def quiz_bot(answer, state):
 
41
 
42
  return "", [(question, feedback)], (q_index, score)
43
 
44
+ # --- TEXT FOR INFORMATION TAB ---
45
+ title = "# 🎤 K-pop Chatbot"
46
+ topics = """👋 Hey K-pop fan!
47
+ Whether you’re new to the fandom or a longtime stan, I’m your ultimate K-pop buddy here to spill all the tea ☕, trivia 🧠, and facts 📚 about your favorite groups and idols! 🌟
48
+
49
+ 💜 Ask me to recommend artists similar to your faves.
50
+ 🎧 Learn about K-pop history, trends, and styles.
51
+ 🚀 Dive into the colorful, energetic world of K-pop!
52
+ """
53
+
54
+ # --- THEME ---
55
+ chat_theme = gr.themes.Soft(
56
+ primary_hue="pink",
57
+ secondary_hue="rose",
58
+ neutral_hue="gray",
59
+ spacing_size="lg",
60
+ radius_size="lg"
61
+ ).set(
62
+ input_background_fill="*neutral_50",
63
+ input_border_color_focus="*primary_300",
64
+ button_primary_background_fill="*primary_500",
65
+ button_primary_background_fill_hover="*primary_400"
66
+ )
67
+
68
+ # --- APP WITH TABS ---
69
+ with gr.Blocks(theme=chat_theme) as demo:
70
  with gr.Tab("Chat"):
71
+ gr.Markdown(title)
72
+ gr.Chatbot(height=400, elem_id="kpop_chat").style(height=400)
73
  msg = gr.Textbox(label="Your message")
74
+ state = gr.State([])
75
+ msg.submit(respond, [msg, state], [msg, state])
76
 
 
 
 
 
77
  with gr.Tab("Quiz"):
78
  quiz_chat = gr.Chatbot()
79
  quiz_input = gr.Textbox(label="Your answer")
80
  start_button = gr.Button("Start Quiz")
81
+ quiz_state = gr.State((0, 0))
82
 
 
 
 
83
  start_button.click(
84
  lambda: [("", [(quiz_questions[0][0], "Type your answer below")]), (0, 0)],
85
  None,
86
  [quiz_input, quiz_chat, quiz_state]
87
  )
 
88
  quiz_input.submit(quiz_bot, [quiz_input, quiz_state], [quiz_input, quiz_chat, quiz_state])
89
 
 
90
  with gr.Tab("Information"):
91
+ gr.Markdown("## ℹ️ About This App")
92
+ gr.Markdown(topics)
 
 
 
 
 
 
 
 
 
 
93
 
94
+ demo.launch(ssr_mode=False)