renatavl commited on
Commit
2cf546e
·
1 Parent(s): 3256847
Files changed (1) hide show
  1. app.py +65 -14
app.py CHANGED
@@ -3,16 +3,58 @@ from __future__ import annotations
3
  import gradio as gr
4
  from agent import ExamAgent
5
 
 
 
 
 
 
6
 
7
  def new_agent() -> ExamAgent:
8
  return ExamAgent()
9
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def on_load():
12
  agent = new_agent()
13
  first = agent.initial_message()
14
- chat_messages = [{"role": "assistant", "content": first}]
15
- return agent, chat_messages
16
 
17
 
18
  def on_reset():
@@ -21,7 +63,7 @@ def on_reset():
21
 
22
  def on_user_message(
23
  agent: ExamAgent,
24
- chat_messages,
25
  user_text: str,
26
  api_key: str,
27
  model: str,
@@ -29,31 +71,35 @@ def on_user_message(
29
  ):
30
  if agent is None:
31
  agent = new_agent()
32
-
33
- if chat_messages is None:
34
- chat_messages = []
35
 
36
  user_text = (user_text or "").strip()
37
  if not user_text:
38
- return agent, chat_messages, ""
39
 
40
- chat_messages.append({"role": "user", "content": user_text})
 
41
 
42
  try:
43
  reply = agent.step(user_text, api_key=api_key, model=model, base_url=base_url)
44
  except Exception as e:
45
  reply = f"Сталася помилка: {e}"
46
 
47
- chat_messages.append({"role": "assistant", "content": reply})
48
- return agent, chat_messages, ""
 
 
 
 
49
 
50
 
51
  with gr.Blocks(title="AI Examiner Agent") as demo:
52
  gr.Markdown(
53
- "# AI Examiner Agent\n"
54
- "Сервіс проводить міні-іспит: питає ім’я та email, обирає 2–3 теми, ставить питання, "
55
- "оцінює відповіді та зберігає результат у файли."
56
- )
57
 
58
  with gr.Row():
59
  api_key = gr.Textbox(label="LLM API Key", type="password", placeholder="Встав ключ тут")
@@ -62,6 +108,9 @@ with gr.Blocks(title="AI Examiner Agent") as demo:
62
 
63
  chatbot = gr.Chatbot(label="Exam Chat", height=420)
64
 
 
 
 
65
  with gr.Row():
66
  user_in = gr.Textbox(label="Твоє повідомлення", placeholder="Напиши відповідь…", scale=4)
67
  send = gr.Button("Send", scale=1)
@@ -76,11 +125,13 @@ with gr.Blocks(title="AI Examiner Agent") as demo:
76
  on_user_message,
77
  inputs=[agent_state, chatbot, user_in, api_key, model, base_url],
78
  outputs=[agent_state, chatbot, user_in],
 
79
  )
80
  user_in.submit(
81
  on_user_message,
82
  inputs=[agent_state, chatbot, user_in, api_key, model, base_url],
83
  outputs=[agent_state, chatbot, user_in],
 
84
  )
85
 
86
  if __name__ == "__main__":
 
3
  import gradio as gr
4
  from agent import ExamAgent
5
 
6
+ try:
7
+ from gradio.exceptions import Error as GradioComponentError
8
+ except Exception:
9
+ GradioComponentError = Exception
10
+
11
 
12
  def new_agent() -> ExamAgent:
13
  return ExamAgent()
14
 
15
 
16
+ CHAT_MODE = "tuples"
17
+
18
+
19
+ def detect_chat_mode(chatbot: gr.Chatbot) -> str:
20
+ """
21
+ Detect what format current gr.Chatbot expects:
22
+ - messages: list[{"role": "...", "content": "..."}]
23
+ - tuples: list[(user, bot)]
24
+ """
25
+ check = getattr(chatbot, "_check_format", None)
26
+
27
+ if callable(check):
28
+ try:
29
+ check([{"role": "assistant", "content": "hi"}])
30
+ return "messages"
31
+ except Exception:
32
+ pass
33
+
34
+ try:
35
+ check([(None, "hi")])
36
+ return "tuples"
37
+ except Exception:
38
+ pass
39
+
40
+ t = getattr(chatbot, "type", None)
41
+ if t in ("messages", "tuples"):
42
+ return t
43
+
44
+ return "tuples"
45
+
46
+
47
+ def init_chat(first_assistant_text: str):
48
+ if CHAT_MODE == "messages":
49
+ return [{"role": "assistant", "content": first_assistant_text}]
50
+ return [(None, first_assistant_text)]
51
+
52
+
53
  def on_load():
54
  agent = new_agent()
55
  first = agent.initial_message()
56
+ chat = init_chat(first)
57
+ return agent, chat
58
 
59
 
60
  def on_reset():
 
63
 
64
  def on_user_message(
65
  agent: ExamAgent,
66
+ chat,
67
  user_text: str,
68
  api_key: str,
69
  model: str,
 
71
  ):
72
  if agent is None:
73
  agent = new_agent()
74
+ if chat is None:
75
+ chat = []
 
76
 
77
  user_text = (user_text or "").strip()
78
  if not user_text:
79
+ return agent, chat, ""
80
 
81
+ if CHAT_MODE == "messages":
82
+ chat.append({"role": "user", "content": user_text})
83
 
84
  try:
85
  reply = agent.step(user_text, api_key=api_key, model=model, base_url=base_url)
86
  except Exception as e:
87
  reply = f"Сталася помилка: {e}"
88
 
89
+ if CHAT_MODE == "messages":
90
+ chat.append({"role": "assistant", "content": reply})
91
+ else:
92
+ chat.append((user_text, reply))
93
+
94
+ return agent, chat, ""
95
 
96
 
97
  with gr.Blocks(title="AI Examiner Agent") as demo:
98
  gr.Markdown(
99
+ "# AI Examiner Agent\n"
100
+ "Сервіс проводить міні-іспит: питає ім’я та email, обирає 2–3 теми, "
101
+ "ставить питання, оцінює відповіді та зберігає результат у файли."
102
+ )
103
 
104
  with gr.Row():
105
  api_key = gr.Textbox(label="LLM API Key", type="password", placeholder="Встав ключ тут")
 
108
 
109
  chatbot = gr.Chatbot(label="Exam Chat", height=420)
110
 
111
+ CHAT_MODE = detect_chat_mode(chatbot)
112
+ print(f"[AI Examiner Agent] Chatbot mode detected: {CHAT_MODE}")
113
+
114
  with gr.Row():
115
  user_in = gr.Textbox(label="Твоє повідомлення", placeholder="Напиши відповідь…", scale=4)
116
  send = gr.Button("Send", scale=1)
 
125
  on_user_message,
126
  inputs=[agent_state, chatbot, user_in, api_key, model, base_url],
127
  outputs=[agent_state, chatbot, user_in],
128
+ api_name=False,
129
  )
130
  user_in.submit(
131
  on_user_message,
132
  inputs=[agent_state, chatbot, user_in, api_key, model, base_url],
133
  outputs=[agent_state, chatbot, user_in],
134
+ api_name=False,
135
  )
136
 
137
  if __name__ == "__main__":