Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,8 @@ torch.set_default_dtype(torch.float32)
|
|
| 12 |
# Set device explicitly
|
| 13 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
|
| 15 |
-
# Load
|
| 16 |
-
model_name = "
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
model_name,
|
|
@@ -75,7 +75,7 @@ def train_model():
|
|
| 75 |
|
| 76 |
# Build RAG index
|
| 77 |
build_rag_index(texts)
|
| 78 |
-
print("Using pretrained
|
| 79 |
|
| 80 |
def add_noise(tensor, sensitivity, epsilon, delta):
|
| 81 |
"""Add Laplace noise for differential privacy."""
|
|
@@ -100,7 +100,7 @@ def chat(message, history):
|
|
| 100 |
if embedder and index:
|
| 101 |
try:
|
| 102 |
query_emb = embedder.encode(message, convert_to_tensor=True).cpu().numpy()
|
| 103 |
-
D, I = index.search(query_emb, k=
|
| 104 |
retrieved = [texts[i] for i in I[0] if i >= 0 and i < len(texts)]
|
| 105 |
context = "\n".join(retrieved)
|
| 106 |
except Exception as e:
|
|
@@ -118,7 +118,7 @@ def chat(message, history):
|
|
| 118 |
input_ids=inputs["input_ids"],
|
| 119 |
attention_mask=inputs["attention_mask"],
|
| 120 |
max_length=100, # Increased for better responses
|
| 121 |
-
num_beams=
|
| 122 |
no_repeat_ngram_size=2,
|
| 123 |
early_stopping=True,
|
| 124 |
)
|
|
@@ -142,8 +142,8 @@ train_model()
|
|
| 142 |
# Gradio interface
|
| 143 |
iface = gr.ChatInterface(
|
| 144 |
fn=chat,
|
| 145 |
-
title="
|
| 146 |
-
description="Chat with pretrained
|
| 147 |
)
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
|
|
|
| 12 |
# Set device explicitly
|
| 13 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
|
| 15 |
+
# Load Persian GPT-2 model and tokenizer
|
| 16 |
+
model_name = "HooshvareLab/gpt2-fa"
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
model_name,
|
|
|
|
| 75 |
|
| 76 |
# Build RAG index
|
| 77 |
build_rag_index(texts)
|
| 78 |
+
print("Using pretrained Persian GPT-2 model without fine-tuning.")
|
| 79 |
|
| 80 |
def add_noise(tensor, sensitivity, epsilon, delta):
|
| 81 |
"""Add Laplace noise for differential privacy."""
|
|
|
|
| 100 |
if embedder and index:
|
| 101 |
try:
|
| 102 |
query_emb = embedder.encode(message, convert_to_tensor=True).cpu().numpy()
|
| 103 |
+
D, I = index.search(query_emb, k=5) # Increased k for better context
|
| 104 |
retrieved = [texts[i] for i in I[0] if i >= 0 and i < len(texts)]
|
| 105 |
context = "\n".join(retrieved)
|
| 106 |
except Exception as e:
|
|
|
|
| 118 |
input_ids=inputs["input_ids"],
|
| 119 |
attention_mask=inputs["attention_mask"],
|
| 120 |
max_length=100, # Increased for better responses
|
| 121 |
+
num_beams=7, # Increased for better quality
|
| 122 |
no_repeat_ngram_size=2,
|
| 123 |
early_stopping=True,
|
| 124 |
)
|
|
|
|
| 142 |
# Gradio interface
|
| 143 |
iface = gr.ChatInterface(
|
| 144 |
fn=chat,
|
| 145 |
+
title="Persian GPT-2 Chatbot with RAG",
|
| 146 |
+
description="Chat with pretrained Persian GPT-2 model using training_data.txt as RAG knowledge base."
|
| 147 |
)
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|