blackanAK06 commited on
Commit
a7b72ab
·
verified ·
1 Parent(s): d77952d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -46
app.py CHANGED
@@ -1,30 +1,13 @@
1
  import gradio as gr
2
  import pdfplumber
3
  import os
4
- from huggingface_hub import HfApi, InferenceClient
5
- import time
6
 
7
- # Thông tin Dataset Hugging Face Token của bạn
8
- DATASET_REPO = "blackanAK06/chatbox-dataset" # Tên dataset của bạn trên Hugging Face
9
- API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN") # Đặt API Token của bạn từ biến môi trường
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, max_retries=3):
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
- # 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():
@@ -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 Dataset Storage",
103
- description="Upload a text file or PDF, save it to a dataset, and ask questions based on its content."
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 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 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