Spaces:
Sleeping
Sleeping
unknown commited on
Commit ·
9840666
0
Parent(s):
changeing script path
Browse files- .gitattributes +35 -0
- Dockerfile +18 -0
- README.md +11 -0
- app2.py +75 -0
- app3.py +86 -0
- requirements.txt +5 -0
- static/script2.js +27 -0
- static/style.css +0 -0
- templates/index2.html +44 -0
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
# Set working directory
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Copy all project files
|
| 7 |
+
COPY . .
|
| 8 |
+
|
| 9 |
+
# Install dependencies
|
| 10 |
+
RUN pip install --upgrade pip
|
| 11 |
+
RUN pip install -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Expose port 7860 as required by Hugging Face
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
# Run the FastAPI app
|
| 17 |
+
CMD ["uvicorn", "app3:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 18 |
+
-port", "7860"]
|
README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: async_chatbot
|
| 3 |
+
emoji: 🌖
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
short_description: testing chatbot with sycin chating
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
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,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
#
|
| 8 |
+
import os
|
| 9 |
+
import json
|
| 10 |
+
from google.oauth2 import service_account
|
| 11 |
+
from google.api_core.client_options import ClientOptions
|
| 12 |
+
|
| 13 |
+
# === Setup
|
| 14 |
+
app = FastAPI()
|
| 15 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 16 |
+
templates = Jinja2Templates(directory="templates")
|
| 17 |
+
|
| 18 |
+
# path_to_key = 'credentials/plenary-stacker-457109-k5-6b664f4fa9ba.json'\
|
| 19 |
+
credentials_info = json.loads(os.getenv("GOOGLE_CREDENTIALS_JSON"))
|
| 20 |
+
credentials = service_account.Credentials.from_service_account_info(credentials_info)
|
| 21 |
+
|
| 22 |
+
api_endpoint_ = 'europe-west2-dialogflow.googleapis.com'
|
| 23 |
+
project_id_ = 'plenary-stacker-457109-k5'
|
| 24 |
+
location_ = 'europe-west2'
|
| 25 |
+
agent_id_ = 'c827abce-ac66-40e5-a7e5-aba7e90b74ae'
|
| 26 |
+
session_id_ = 'unique-session-id'
|
| 27 |
+
|
| 28 |
+
clt1 = None
|
| 29 |
+
s_path = None
|
| 30 |
+
|
| 31 |
+
def set_session():
|
| 32 |
+
# client1 = dialogflow.SessionsClient(
|
| 33 |
+
# credentials=service_account.Credentials.from_service_account_file(path_to_key),
|
| 34 |
+
# client_options=ClientOptions(api_endpoint=api_endpoint_)
|
| 35 |
+
# )
|
| 36 |
+
client1 = dialogflow.SessionsClient(
|
| 37 |
+
credentials=credentials,
|
| 38 |
+
client_options=ClientOptions(api_endpoint=api_endpoint_)
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
session_path1 = client1.session_path(project_id_, location_, agent_id_, session_id_)
|
| 42 |
+
return client1, session_path1
|
| 43 |
+
|
| 44 |
+
async def get_response(message_):
|
| 45 |
+
global clt1, s_path
|
| 46 |
+
if clt1 is None or s_path is None:
|
| 47 |
+
clt1, s_path = set_session()
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
query_input = dialogflow.QueryInput(
|
| 51 |
+
text=dialogflow.TextInput(text=message_),
|
| 52 |
+
language_code='en'
|
| 53 |
+
)
|
| 54 |
+
loop = asyncio.get_event_loop()
|
| 55 |
+
response = await loop.run_in_executor(
|
| 56 |
+
None,
|
| 57 |
+
lambda: clt1.detect_intent(request={"session": s_path, "query_input": query_input})
|
| 58 |
+
)
|
| 59 |
+
return extract_message(response)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f'Error: {e}'
|
| 62 |
+
|
| 63 |
+
def extract_message(response):
|
| 64 |
+
response_messages = response.query_result.response_messages
|
| 65 |
+
message_text = ''
|
| 66 |
+
for message in response_messages:
|
| 67 |
+
if message.text and hasattr(message.text.text, '__iter__'):
|
| 68 |
+
message_text += ''.join(message.text.text)
|
| 69 |
+
if not message_text:
|
| 70 |
+
message_text = response.query_result.fulfillment_text
|
| 71 |
+
return message_text
|
| 72 |
+
|
| 73 |
+
@app.get("/", response_class=HTMLResponse)
|
| 74 |
+
async def index(request: Request):
|
| 75 |
+
return templates.TemplateResponse("index2.html", {"request": request})
|
| 76 |
+
|
| 77 |
+
@app.post("/chat", response_class=JSONResponse)
|
| 78 |
+
async def chat_post(request: Request):
|
| 79 |
+
data = await request.json()
|
| 80 |
+
user_input = data.get("message", "")
|
| 81 |
+
# user_input = 'hey man'
|
| 82 |
+
response_text = await get_response(user_input)
|
| 83 |
+
return {"reply": response_text}
|
| 84 |
+
|
| 85 |
+
# if __name__ == '__main__':
|
| 86 |
+
# app.run(debug=True)
|
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,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Async Chatbot</title>
|
| 5 |
+
<!-- <script src="/static/script2.js" defer></script> -->
|
| 6 |
+
<script >
|
| 7 |
+
async function sendMessage() {
|
| 8 |
+
const inputEl = document.getElementById("userInput");
|
| 9 |
+
const statusEl = document.getElementById("status");
|
| 10 |
+
const replyEl = document.getElementById("botReply");
|
| 11 |
+
|
| 12 |
+
const message = inputEl.value.trim();
|
| 13 |
+
if (!message) return;
|
| 14 |
+
|
| 15 |
+
// Update status
|
| 16 |
+
statusEl.innerText = "Connecting to service...";
|
| 17 |
+
await delay(300);
|
| 18 |
+
|
| 19 |
+
statusEl.innerText = "Getting data...";
|
| 20 |
+
const response = await fetch("/chat", {
|
| 21 |
+
method: "POST",
|
| 22 |
+
headers: { "Content-Type": "application/json" },
|
| 23 |
+
body: JSON.stringify({ message })
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
const data = await response.json();
|
| 27 |
+
statusEl.innerText = "Operation complete.";
|
| 28 |
+
replyEl.innerText = data.reply;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
function delay(ms) {
|
| 32 |
+
return new Promise(resolve => setTimeout(resolve, ms));
|
| 33 |
+
}
|
| 34 |
+
</script>
|
| 35 |
+
</head>
|
| 36 |
+
<body>
|
| 37 |
+
<h2>Talk to the Bot</h2>
|
| 38 |
+
<input type="text" id="userInput" placeholder="Say something..." />
|
| 39 |
+
<button onclick="sendMessage()">Send</button>
|
| 40 |
+
|
| 41 |
+
<p><strong>Status:</strong> <span id="status">Idle</span></p>
|
| 42 |
+
<p><strong>Bot:</strong> <span id="botReply"></span></p>
|
| 43 |
+
</body>
|
| 44 |
+
</html>
|