Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pdfplumber
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Khởi tạo Hugging Face API và InferenceClient với mô hình GPT-Neo
|
| 12 |
-
api = HfApi()
|
| 13 |
-
client = InferenceClient(model="EleutherAI/gpt-neo-2.7B", token=API_TOKEN) # Khởi tạo InferenceClient với mô hình cụ thể
|
| 14 |
-
|
| 15 |
-
# Hàm để tải lên dataset
|
| 16 |
-
def upload_to_dataset(file_path, filename):
|
| 17 |
-
try:
|
| 18 |
-
api.upload_file(
|
| 19 |
-
path_or_fileobj=file_path,
|
| 20 |
-
path_in_repo=f"files/{filename}",
|
| 21 |
-
repo_id=DATASET_REPO,
|
| 22 |
-
repo_type="dataset",
|
| 23 |
-
token=API_TOKEN,
|
| 24 |
-
)
|
| 25 |
-
return f"Successfully uploaded {filename} to the dataset."
|
| 26 |
-
except Exception as e:
|
| 27 |
-
return f"Error uploading file: {e}"
|
| 28 |
|
| 29 |
# Hàm để trích xuất văn bản từ tệp PDF
|
| 30 |
def extract_text_from_pdf(file_path):
|
|
@@ -40,17 +23,12 @@ def extract_text_from_pdf(file_path):
|
|
| 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
|
| 44 |
if not message:
|
| 45 |
return "Please enter a question."
|
| 46 |
|
| 47 |
filename = os.path.basename(filepath)
|
| 48 |
|
| 49 |
-
# Tải tệp lên dataset
|
| 50 |
-
upload_message = upload_to_dataset(filepath, filename)
|
| 51 |
-
if "Error" in upload_message:
|
| 52 |
-
return upload_message
|
| 53 |
-
|
| 54 |
# Trích xuất nội dung từ tệp đã tải lên
|
| 55 |
if filename.endswith(".pdf"):
|
| 56 |
file_content = extract_text_from_pdf(filepath)
|
|
@@ -68,21 +46,15 @@ def respond_with_file(filepath, message, max_retries=3):
|
|
| 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 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 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():
|
|
@@ -99,8 +71,8 @@ def interface():
|
|
| 99 |
outputs="text",
|
| 100 |
live=False,
|
| 101 |
flagging_mode="never", # Thay thế allow_flagging bằng flagging_mode
|
| 102 |
-
title="File-based Chatbot with
|
| 103 |
-
description="Upload a text file or PDF,
|
| 104 |
)
|
| 105 |
return demo
|
| 106 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pdfplumber
|
| 3 |
import os
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
|
| 7 |
+
# Tải mô hình từ Hugging Face Model Hub
|
| 8 |
+
model_name = "antphb/DS-Chatbox-facebook-xglm-564M-V4-FT"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Hàm để trích xuất văn bản từ tệp PDF
|
| 13 |
def extract_text_from_pdf(file_path):
|
|
|
|
| 23 |
return text
|
| 24 |
|
| 25 |
# Hàm để xử lý câu hỏi và nội dung tệp sau khi người dùng yêu cầu
|
| 26 |
+
def respond_with_file(filepath, message):
|
| 27 |
if not message:
|
| 28 |
return "Please enter a question."
|
| 29 |
|
| 30 |
filename = os.path.basename(filepath)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Trích xuất nội dung từ tệp đã tải lên
|
| 33 |
if filename.endswith(".pdf"):
|
| 34 |
file_content = extract_text_from_pdf(filepath)
|
|
|
|
| 46 |
# Kết hợp nội dung tệp với câu hỏi để mô hình trả lời
|
| 47 |
full_message = f"Here is the content of the file:\n{file_content}\n\nUser question: {message}"
|
| 48 |
|
| 49 |
+
# Sử dụng mô hình để tạo phản hồi
|
| 50 |
+
try:
|
| 51 |
+
inputs = tokenizer(full_message, return_tensors="pt")
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
outputs = model.generate(**inputs, max_new_tokens=150, temperature=0.7)
|
| 54 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 55 |
+
return f"Response:\n{generated_text}"
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Error generating response from the model: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Giao diện Gradio
|
| 60 |
def interface():
|
|
|
|
| 71 |
outputs="text",
|
| 72 |
live=False,
|
| 73 |
flagging_mode="never", # Thay thế allow_flagging bằng flagging_mode
|
| 74 |
+
title="File-based Chatbot with Direct Model Access",
|
| 75 |
+
description="Upload a text file or PDF, and ask questions based on its content using a transformer model."
|
| 76 |
)
|
| 77 |
return demo
|
| 78 |
|