ehsanhon commited on
Commit
3cdd237
·
verified ·
1 Parent(s): a5f0558

Upload folder using huggingface_hub

Browse files
Files changed (7) hide show
  1. app2.py +75 -0
  2. app3.py +75 -0
  3. dockerfile +0 -0
  4. requirements.txt +5 -0
  5. static/script2.js +27 -0
  6. static/style.css +0 -0
  7. templates/index2.html +15 -0
app2.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import HTMLResponse, JSONResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ from fastapi.templating import Jinja2Templates
5
+ import asyncio
6
+ from google.cloud import dialogflowcx_v3 as dialogflow
7
+ from google.oauth2 import service_account
8
+ from google.api_core.client_options import ClientOptions
9
+
10
+ # === Setup
11
+ app = FastAPI()
12
+ app.mount("/static", StaticFiles(directory="static"), name="static")
13
+ templates = Jinja2Templates(directory="templates")
14
+
15
+ path_to_key = 'credentials/plenary-stacker-457109-k5-6b664f4fa9ba.json'
16
+ api_endpoint_ = 'europe-west2-dialogflow.googleapis.com'
17
+ project_id_ = 'plenary-stacker-457109-k5'
18
+ location_ = 'europe-west2'
19
+ agent_id_ = 'c827abce-ac66-40e5-a7e5-aba7e90b74ae'
20
+ session_id_ = 'unique-session-id'
21
+
22
+ clt1 = None
23
+ s_path = None
24
+
25
+ def set_session():
26
+ client1 = dialogflow.SessionsClient(
27
+ credentials=service_account.Credentials.from_service_account_file(path_to_key),
28
+ client_options=ClientOptions(api_endpoint=api_endpoint_)
29
+ )
30
+ session_path1 = client1.session_path(project_id_, location_, agent_id_, session_id_)
31
+ return client1, session_path1
32
+
33
+ async def get_response(message_):
34
+ global clt1, s_path
35
+ if clt1 is None or s_path is None:
36
+ clt1, s_path = set_session()
37
+
38
+ try:
39
+ query_input = dialogflow.QueryInput(
40
+ text=dialogflow.TextInput(text=message_),
41
+ language_code='en'
42
+ )
43
+ loop = asyncio.get_event_loop()
44
+ response = await loop.run_in_executor(
45
+ None,
46
+ lambda: clt1.detect_intent(request={"session": s_path, "query_input": query_input})
47
+ )
48
+ return extract_message(response)
49
+ except Exception as e:
50
+ return f'Error: {e}'
51
+
52
+ def extract_message(response):
53
+ response_messages = response.query_result.response_messages
54
+ message_text = ''
55
+ for message in response_messages:
56
+ if message.text and hasattr(message.text.text, '__iter__'):
57
+ message_text += ''.join(message.text.text)
58
+ if not message_text:
59
+ message_text = response.query_result.fulfillment_text
60
+ return message_text
61
+
62
+ @app.get("/", response_class=HTMLResponse)
63
+ async def index(request: Request):
64
+ return templates.TemplateResponse("index2.html", {"request": request})
65
+
66
+ @app.post("/chat", response_class=JSONResponse)
67
+ async def chat_post(request: Request):
68
+ data = await request.json()
69
+ user_input = data.get("message", "")
70
+ # user_input = 'hey man'
71
+ response_text = await get_response(user_input)
72
+ return {"reply": response_text}
73
+
74
+ # if __name__ == '__main__':
75
+ # app.run(debug=True)
app3.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import HTMLResponse, JSONResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ from fastapi.templating import Jinja2Templates
5
+ import asyncio
6
+ from google.cloud import dialogflowcx_v3 as dialogflow
7
+ from google.oauth2 import service_account
8
+ from google.api_core.client_options import ClientOptions
9
+
10
+ # === Setup
11
+ app = FastAPI()
12
+ app.mount("/static", StaticFiles(directory="static"), name="static")
13
+ templates = Jinja2Templates(directory="templates")
14
+
15
+ path_to_key = 'credentials/plenary-stacker-457109-k5-6b664f4fa9ba.json'
16
+ api_endpoint_ = 'europe-west2-dialogflow.googleapis.com'
17
+ project_id_ = 'plenary-stacker-457109-k5'
18
+ location_ = 'europe-west2'
19
+ agent_id_ = 'c827abce-ac66-40e5-a7e5-aba7e90b74ae'
20
+ session_id_ = 'unique-session-id'
21
+
22
+ clt1 = None
23
+ s_path = None
24
+
25
+ def set_session():
26
+ client1 = dialogflow.SessionsClient(
27
+ credentials=service_account.Credentials.from_service_account_file(path_to_key),
28
+ client_options=ClientOptions(api_endpoint=api_endpoint_)
29
+ )
30
+ session_path1 = client1.session_path(project_id_, location_, agent_id_, session_id_)
31
+ return client1, session_path1
32
+
33
+ async def get_response(message_):
34
+ global clt1, s_path
35
+ if clt1 is None or s_path is None:
36
+ clt1, s_path = set_session()
37
+
38
+ try:
39
+ query_input = dialogflow.QueryInput(
40
+ text=dialogflow.TextInput(text=message_),
41
+ language_code='en'
42
+ )
43
+ loop = asyncio.get_event_loop()
44
+ response = await loop.run_in_executor(
45
+ None,
46
+ lambda: clt1.detect_intent(request={"session": s_path, "query_input": query_input})
47
+ )
48
+ return extract_message(response)
49
+ except Exception as e:
50
+ return f'Error: {e}'
51
+
52
+ def extract_message(response):
53
+ response_messages = response.query_result.response_messages
54
+ message_text = ''
55
+ for message in response_messages:
56
+ if message.text and hasattr(message.text.text, '__iter__'):
57
+ message_text += ''.join(message.text.text)
58
+ if not message_text:
59
+ message_text = response.query_result.fulfillment_text
60
+ return message_text
61
+
62
+ @app.get("/", response_class=HTMLResponse)
63
+ async def index(request: Request):
64
+ return templates.TemplateResponse("index2.html", {"request": request})
65
+
66
+ @app.post("/chat", response_class=JSONResponse)
67
+ async def chat_post(request: Request):
68
+ data = await request.json()
69
+ user_input = data.get("message", "")
70
+ # user_input = 'hey man'
71
+ response_text = await get_response(user_input)
72
+ return {"reply": response_text}
73
+
74
+ # if __name__ == '__main__':
75
+ # app.run(debug=True)
dockerfile ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi==0.110.0
2
+ uvicorn==0.29.0
3
+ jinja2==3.1.3
4
+ google-cloud-dialogflow-cx==1.14.1
5
+ google-auth==2.29.0
static/script2.js ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ async function sendMessage() {
2
+ const inputEl = document.getElementById("userInput");
3
+ const statusEl = document.getElementById("status");
4
+ const replyEl = document.getElementById("botReply");
5
+
6
+ const message = inputEl.value.trim();
7
+ if (!message) return;
8
+
9
+ // Update status
10
+ statusEl.innerText = "Connecting to service...";
11
+ await delay(300);
12
+
13
+ statusEl.innerText = "Getting data...";
14
+ const response = await fetch("/chat", {
15
+ method: "POST",
16
+ headers: { "Content-Type": "application/json" },
17
+ body: JSON.stringify({ message })
18
+ });
19
+
20
+ const data = await response.json();
21
+ statusEl.innerText = "Operation complete.";
22
+ replyEl.innerText = data.reply;
23
+ }
24
+
25
+ function delay(ms) {
26
+ return new Promise(resolve => setTimeout(resolve, ms));
27
+ }
static/style.css ADDED
File without changes
templates/index2.html ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Async Chatbot</title>
5
+ <script src="/static/script2.js" defer></script>
6
+ </head>
7
+ <body>
8
+ <h2>Talk to the Bot</h2>
9
+ <input type="text" id="userInput" placeholder="Say something..." />
10
+ <button onclick="sendMessage()">Send</button>
11
+
12
+ <p><strong>Status:</strong> <span id="status">Idle</span></p>
13
+ <p><strong>Bot:</strong> <span id="botReply"></span></p>
14
+ </body>
15
+ </html>