Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,16 +6,14 @@ import pandas as pd
|
|
| 6 |
import os
|
| 7 |
import torch
|
| 8 |
|
| 9 |
-
# Check
|
| 10 |
print("CUDA available:", torch.cuda.is_available())
|
| 11 |
-
|
| 12 |
if torch.cuda.is_available():
|
| 13 |
print("GPU Name:", torch.cuda.get_device_name(0))
|
| 14 |
else:
|
| 15 |
print("No GPU detected. Running on CPU.")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
# Set a seed for reproducibility
|
| 19 |
set_seed(42)
|
| 20 |
|
| 21 |
# Define the six premium generation models:
|
|
@@ -43,20 +41,20 @@ grammar_model_names = [
|
|
| 43 |
"hassaanik/grammar-correction-model"
|
| 44 |
]
|
| 45 |
|
| 46 |
-
#
|
|
|
|
|
|
|
|
|
|
| 47 |
def load_generation_pipeline(model_name):
|
| 48 |
try:
|
| 49 |
-
# Use device=0 if GPU is available; otherwise, use CPU (device=-1)
|
| 50 |
-
device = 0 if torch.cuda.is_available() else -1
|
| 51 |
return pipeline("text-generation", model=model_name, device=device)
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Error loading generation model {model_name}: {e}")
|
| 54 |
return None
|
| 55 |
|
| 56 |
-
# Function to load grammar evaluation pipelines.
|
| 57 |
def load_grammar_pipeline(model_name):
|
| 58 |
try:
|
| 59 |
-
device = 0 if torch.cuda.is_available() else -1
|
| 60 |
return pipeline("text2text-generation", model=model_name, device=device)
|
| 61 |
except Exception as e:
|
| 62 |
print(f"Error loading grammar model {model_name}: {e}")
|
|
@@ -76,12 +74,12 @@ def is_palindrome(text):
|
|
| 76 |
cleaned = clean_text(text)
|
| 77 |
return cleaned == cleaned[::-1]
|
| 78 |
|
| 79 |
-
# Updated prompt
|
| 80 |
def build_prompt(lang):
|
| 81 |
return (
|
| 82 |
f"Instruction: Generate a single original palindrome in {lang}.\n"
|
| 83 |
"Output only the palindrome. The palindrome should be a continuous text that reads the same forward and backward.\n"
|
| 84 |
-
"Do not output any additional text
|
| 85 |
"Palindrome: "
|
| 86 |
)
|
| 87 |
|
|
@@ -144,6 +142,7 @@ def run_benchmark_all():
|
|
| 144 |
print(f"CSV saved to {os.path.abspath(csv_path)}")
|
| 145 |
return gr.Dataframe(df), csv_path
|
| 146 |
|
|
|
|
| 147 |
with gr.Blocks(title="Premium Model Palindrome Benchmark") as demo:
|
| 148 |
gr.Markdown("# Premium Model Palindrome Benchmark")
|
| 149 |
gr.Markdown(
|
|
|
|
| 6 |
import os
|
| 7 |
import torch
|
| 8 |
|
| 9 |
+
# Check GPU availability (for debugging)
|
| 10 |
print("CUDA available:", torch.cuda.is_available())
|
|
|
|
| 11 |
if torch.cuda.is_available():
|
| 12 |
print("GPU Name:", torch.cuda.get_device_name(0))
|
| 13 |
else:
|
| 14 |
print("No GPU detected. Running on CPU.")
|
| 15 |
|
| 16 |
+
# Set seed for reproducibility
|
|
|
|
| 17 |
set_seed(42)
|
| 18 |
|
| 19 |
# Define the six premium generation models:
|
|
|
|
| 41 |
"hassaanik/grammar-correction-model"
|
| 42 |
]
|
| 43 |
|
| 44 |
+
# Determine device: Use GPU (0) if available, otherwise CPU (-1)
|
| 45 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 46 |
+
|
| 47 |
+
# Function to load generation pipelines with appropriate device setting.
|
| 48 |
def load_generation_pipeline(model_name):
|
| 49 |
try:
|
|
|
|
|
|
|
| 50 |
return pipeline("text-generation", model=model_name, device=device)
|
| 51 |
except Exception as e:
|
| 52 |
print(f"Error loading generation model {model_name}: {e}")
|
| 53 |
return None
|
| 54 |
|
| 55 |
+
# Function to load grammar evaluation pipelines with appropriate device setting.
|
| 56 |
def load_grammar_pipeline(model_name):
|
| 57 |
try:
|
|
|
|
| 58 |
return pipeline("text2text-generation", model=model_name, device=device)
|
| 59 |
except Exception as e:
|
| 60 |
print(f"Error loading grammar model {model_name}: {e}")
|
|
|
|
| 74 |
cleaned = clean_text(text)
|
| 75 |
return cleaned == cleaned[::-1]
|
| 76 |
|
| 77 |
+
# Updated prompt instructs the model to output only the palindrome.
|
| 78 |
def build_prompt(lang):
|
| 79 |
return (
|
| 80 |
f"Instruction: Generate a single original palindrome in {lang}.\n"
|
| 81 |
"Output only the palindrome. The palindrome should be a continuous text that reads the same forward and backward.\n"
|
| 82 |
+
"Do not output any additional text or commentary.\n"
|
| 83 |
"Palindrome: "
|
| 84 |
)
|
| 85 |
|
|
|
|
| 142 |
print(f"CSV saved to {os.path.abspath(csv_path)}")
|
| 143 |
return gr.Dataframe(df), csv_path
|
| 144 |
|
| 145 |
+
# Build the Gradio UI using Blocks for a canvas layout.
|
| 146 |
with gr.Blocks(title="Premium Model Palindrome Benchmark") as demo:
|
| 147 |
gr.Markdown("# Premium Model Palindrome Benchmark")
|
| 148 |
gr.Markdown(
|