Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import pdfplumber
|
| 3 |
import os
|
| 4 |
from huggingface_hub import HfApi, InferenceClient
|
|
|
|
| 5 |
|
| 6 |
# Thông tin Dataset và Hugging Face Token của bạn
|
| 7 |
DATASET_REPO = "blackanAK06/chatbox-dataset" # Tên dataset của bạn trên Hugging Face
|
|
@@ -39,7 +40,7 @@ def extract_text_from_pdf(file_path):
|
|
| 39 |
return text
|
| 40 |
|
| 41 |
# Hàm để xử lý câu hỏi và nội dung tệp sau khi người dùng yêu cầu
|
| 42 |
-
def respond_with_file(filepath, message):
|
| 43 |
if not message:
|
| 44 |
return "Please enter a question."
|
| 45 |
|
|
@@ -67,14 +68,21 @@ def respond_with_file(filepath, message):
|
|
| 67 |
# Kết hợp nội dung tệp với câu hỏi để mô hình trả lời
|
| 68 |
full_message = f"Here is the content of the file:\n{file_content}\n\nUser question: {message}"
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
# Giao diện Gradio
|
| 80 |
def interface():
|
|
|
|
| 2 |
import pdfplumber
|
| 3 |
import os
|
| 4 |
from huggingface_hub import HfApi, InferenceClient
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
# Thông tin Dataset và Hugging Face Token của bạn
|
| 8 |
DATASET_REPO = "blackanAK06/chatbox-dataset" # Tên dataset của bạn trên Hugging Face
|
|
|
|
| 40 |
return text
|
| 41 |
|
| 42 |
# Hàm để xử lý câu hỏi và nội dung tệp sau khi người dùng yêu cầu
|
| 43 |
+
def respond_with_file(filepath, message, max_retries=3):
|
| 44 |
if not message:
|
| 45 |
return "Please enter a question."
|
| 46 |
|
|
|
|
| 68 |
# Kết hợp nội dung tệp với câu hỏi để mô hình trả lời
|
| 69 |
full_message = f"Here is the content of the file:\n{file_content}\n\nUser question: {message}"
|
| 70 |
|
| 71 |
+
# Cơ chế thử lại nếu gặp lỗi
|
| 72 |
+
for attempt in range(max_retries):
|
| 73 |
+
try:
|
| 74 |
+
# Gửi yêu cầu tới mô hình GPT-Neo để tạo ra câu trả lời
|
| 75 |
+
response = client.text_generation(full_message, max_new_tokens=150, temperature=0.7)
|
| 76 |
+
return f"{upload_message}\n\nResponse:\n{response}"
|
| 77 |
+
except Exception as e:
|
| 78 |
+
if "CANCELLED" in str(e):
|
| 79 |
+
time.sleep(2) # Chờ 2 giây trước khi thử lại
|
| 80 |
+
continue
|
| 81 |
+
else:
|
| 82 |
+
return f"Error communicating with the model: {e}"
|
| 83 |
|
| 84 |
+
# Nếu hết số lần thử mà vẫn lỗi
|
| 85 |
+
return "Request failed after several attempts. Please try again later."
|
| 86 |
|
| 87 |
# Giao diện Gradio
|
| 88 |
def interface():
|