Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,12 @@
|
|
| 3 |
# -----------------------------------------------------------
|
| 4 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 5 |
from evaluate import load
|
| 6 |
-
from llama_cpp import Llama
|
| 7 |
|
| 8 |
# Pipeline for easy text generation
|
| 9 |
pipe = pipeline(
|
| 10 |
"text-generation",
|
| 11 |
model="varma007ut/Indian_Legal_Assitant",
|
| 12 |
-
|
| 13 |
max_length=200
|
| 14 |
)
|
| 15 |
|
|
@@ -21,7 +20,7 @@ print(result[0]["generated_text"])
|
|
| 21 |
|
| 22 |
|
| 23 |
# -----------------------------------------------------------
|
| 24 |
-
# 2. TEXT GENERATION USING
|
| 25 |
# -----------------------------------------------------------
|
| 26 |
|
| 27 |
tokenizer = AutoTokenizer.from_pretrained("varma007ut/Indian_Legal_Assitant")
|
|
@@ -30,7 +29,13 @@ model = AutoModelForCausalLM.from_pretrained("varma007ut/Indian_Legal_Assitant")
|
|
| 30 |
prompt2 = "What are the fundamental rights in the Indian Constitution?"
|
| 31 |
inputs = tokenizer(prompt2, return_tensors="pt")
|
| 32 |
|
| 33 |
-
outputs = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
|
| 36 |
print("\n=== HF Model.generate Output ===")
|
|
@@ -38,39 +43,17 @@ print(generated_text)
|
|
| 38 |
|
| 39 |
|
| 40 |
# -----------------------------------------------------------
|
| 41 |
-
# 3.
|
| 42 |
-
# -----------------------------------------------------------
|
| 43 |
-
|
| 44 |
-
llm = Llama(
|
| 45 |
-
model_path="./ggml-model-q4_0.gguf", # Ensure the file exists!
|
| 46 |
-
n_ctx=4096
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
response = llm.create_chat_completion(
|
| 50 |
-
messages=[
|
| 51 |
-
{
|
| 52 |
-
"role": "user",
|
| 53 |
-
"content": "Explain the concept of judicial review in India."
|
| 54 |
-
}
|
| 55 |
-
]
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
print("\n=== LLaMA.cpp Output ===")
|
| 59 |
-
print(response["choices"][0]["message"]["content"])
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# -----------------------------------------------------------
|
| 63 |
-
# 4. BLEU SCORE EVALUATION (CLEAN + CORRECT)
|
| 64 |
# -----------------------------------------------------------
|
| 65 |
|
| 66 |
bleu = load("bleu")
|
| 67 |
|
| 68 |
-
|
| 69 |
-
pred_text = generated_text # using output from HF model
|
| 70 |
|
| 71 |
-
# Reference answer (
|
| 72 |
-
references = ["Fundamental Rights are guaranteed by the Constitution.
|
| 73 |
|
|
|
|
| 74 |
results = bleu.compute(
|
| 75 |
predictions=[pred_text.split()],
|
| 76 |
references=[[ref.split() for ref in references]]
|
|
@@ -78,4 +61,3 @@ results = bleu.compute(
|
|
| 78 |
|
| 79 |
print("\n=== BLEU Score ===")
|
| 80 |
print(results)
|
| 81 |
-
|
|
|
|
| 3 |
# -----------------------------------------------------------
|
| 4 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 5 |
from evaluate import load
|
|
|
|
| 6 |
|
| 7 |
# Pipeline for easy text generation
|
| 8 |
pipe = pipeline(
|
| 9 |
"text-generation",
|
| 10 |
model="varma007ut/Indian_Legal_Assitant",
|
| 11 |
+
device="cpu",
|
| 12 |
max_length=200
|
| 13 |
)
|
| 14 |
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
# -----------------------------------------------------------
|
| 23 |
+
# 2. TEXT GENERATION USING model.generate()
|
| 24 |
# -----------------------------------------------------------
|
| 25 |
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained("varma007ut/Indian_Legal_Assitant")
|
|
|
|
| 29 |
prompt2 = "What are the fundamental rights in the Indian Constitution?"
|
| 30 |
inputs = tokenizer(prompt2, return_tensors="pt")
|
| 31 |
|
| 32 |
+
outputs = model.generate(
|
| 33 |
+
**inputs,
|
| 34 |
+
max_length=200,
|
| 35 |
+
do_sample=True,
|
| 36 |
+
top_p=0.95
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 40 |
|
| 41 |
print("\n=== HF Model.generate Output ===")
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
# -----------------------------------------------------------
|
| 46 |
+
# 3. BLEU SCORE EVALUATION (SAFE FOR SPACES)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# -----------------------------------------------------------
|
| 48 |
|
| 49 |
bleu = load("bleu")
|
| 50 |
|
| 51 |
+
pred_text = generated_text # prediction from HF model
|
|
|
|
| 52 |
|
| 53 |
+
# Reference answer (your true dataset reference goes here)
|
| 54 |
+
references = ["Fundamental Rights are guaranteed by the Indian Constitution."]
|
| 55 |
|
| 56 |
+
# BLEU expects tokenized text (split into words)
|
| 57 |
results = bleu.compute(
|
| 58 |
predictions=[pred_text.split()],
|
| 59 |
references=[[ref.split() for ref in references]]
|
|
|
|
| 61 |
|
| 62 |
print("\n=== BLEU Score ===")
|
| 63 |
print(results)
|
|
|