-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
75 lines (54 loc) · 1.56 KB
/
Copy pathcli.py
File metadata and controls
75 lines (54 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, Input, RichLog
from chatbot import get_answer
import asyncio
class ChatCLI(App):
CSS = """
Screen {
background: black;
}
#chat {
border: round cyan;
padding: 1;
}
Input {
border: round green;
}
"""
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield RichLog(id="chat", wrap=True)
yield Input(placeholder="Type your message...")
yield Footer()
# =========================
# INPUT HANDLER
# =========================
def on_input_submitted(self, event: Input.Submitted):
user_text = event.value.strip()
if not user_text:
return
chat = self.query_one(RichLog)
# USER MESSAGE
chat.write(f"You: {user_text}")
# CLEAR INPUT
event.input.value = ""
# RUN AI ASYNC (IMPORTANT FIX)
asyncio.create_task(self.ask_ai(user_text))
# =========================
# AI HANDLER
# =========================
async def ask_ai(self, text: str):
chat = self.query_one(RichLog)
# LOADING MESSAGE
chat.write(" Bot is thinking... ")
try:
# CALL YOUR AI FUNCTION
answer = await get_answer(text)
chat.write(f" Bot: {answer}")
except Exception as e:
chat.write(f" Error: {e}")
# =========================
# RUN APP
# =========================
if __name__ == "__main__":
ChatCLI().run()