Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,199 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
model_name = "WYNN747/Burmese-GPT-v3"
|
6 |
-
|
7 |
-
model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def generate_text(prompt, max_length=100, temperature=0.7):
|
10 |
"""Generate text based on the input prompt."""
|
11 |
-
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
)
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
# Launch the app
|
40 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import time
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
from huggingface_hub import HfApi
|
6 |
+
import requests
|
7 |
+
from requests.adapters import HTTPAdapter
|
8 |
+
from urllib3.util.retry import Retry
|
9 |
|
10 |
+
# Configure requests to be more resilient
|
11 |
+
retry_strategy = Retry(
|
12 |
+
total=5,
|
13 |
+
backoff_factor=1,
|
14 |
+
status_forcelist=[429, 500, 502, 503, 504],
|
15 |
+
allowed_methods=["HEAD", "GET", "OPTIONS"]
|
16 |
+
)
|
17 |
+
adapter = HTTPAdapter(max_retries=retry_strategy)
|
18 |
+
session = requests.Session()
|
19 |
+
session.mount("https://", adapter)
|
20 |
+
session.mount("http://", adapter)
|
21 |
+
|
22 |
+
# Set longer timeout for model downloads
|
23 |
+
os.environ["HF_HUB_DOWNLOAD_TIMEOUT"] = "600" # 10 minutes timeout
|
24 |
+
|
25 |
+
# Model name
|
26 |
model_name = "WYNN747/Burmese-GPT-v3"
|
27 |
+
|
28 |
+
# Function to load model with retries
|
29 |
+
def load_model_with_retries(model_name, max_retries=3, retry_delay=5):
|
30 |
+
for attempt in range(max_retries):
|
31 |
+
try:
|
32 |
+
print(f"Loading model attempt {attempt+1}/{max_retries}")
|
33 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
34 |
+
model_name,
|
35 |
+
use_fast=False, # Sometimes the fast tokenizer causes issues
|
36 |
+
local_files_only=False,
|
37 |
+
token=os.environ.get("HF_TOKEN", None), # Use token if available
|
38 |
+
trust_remote_code=True,
|
39 |
+
timeout=600 # 10 minutes timeout
|
40 |
+
)
|
41 |
+
|
42 |
+
model = AutoModelForCausalLM.from_pretrained(
|
43 |
+
model_name,
|
44 |
+
local_files_only=False,
|
45 |
+
token=os.environ.get("HF_TOKEN", None),
|
46 |
+
trust_remote_code=True,
|
47 |
+
timeout=600, # 10 minutes timeout
|
48 |
+
low_cpu_mem_usage=True, # Help with memory issues
|
49 |
+
torch_dtype="auto" # Use appropriate dtype
|
50 |
+
)
|
51 |
+
return tokenizer, model
|
52 |
+
except (requests.exceptions.ReadTimeout, requests.exceptions.ConnectionError) as e:
|
53 |
+
if attempt < max_retries - 1:
|
54 |
+
print(f"Timeout error: {str(e)}. Retrying in {retry_delay} seconds...")
|
55 |
+
time.sleep(retry_delay)
|
56 |
+
retry_delay *= 2 # Exponential backoff
|
57 |
+
else:
|
58 |
+
raise Exception(f"Failed to load model after {max_retries} attempts: {str(e)}")
|
59 |
+
except Exception as e:
|
60 |
+
raise Exception(f"Error loading model: {str(e)}")
|
61 |
+
|
62 |
+
# Load model
|
63 |
+
try:
|
64 |
+
tokenizer, model = load_model_with_retries(model_name)
|
65 |
+
print("Model loaded successfully!")
|
66 |
+
except Exception as e:
|
67 |
+
print(f"Error loading model: {str(e)}")
|
68 |
+
# Create placeholder objects for UI to start
|
69 |
+
# This allows the UI to start even if model loading fails initially
|
70 |
+
tokenizer = None
|
71 |
+
model = None
|
72 |
|
73 |
def generate_text(prompt, max_length=100, temperature=0.7):
|
74 |
"""Generate text based on the input prompt."""
|
75 |
+
global tokenizer, model
|
76 |
+
|
77 |
+
# Check if model is loaded
|
78 |
+
if tokenizer is None or model is None:
|
79 |
+
try:
|
80 |
+
# Try loading the model again if it failed initially
|
81 |
+
tokenizer, model = load_model_with_retries(model_name)
|
82 |
+
print("Model loaded on demand")
|
83 |
+
except Exception as e:
|
84 |
+
return f"Error: Model could not be loaded. Please check your internet connection and try again. Details: {str(e)}"
|
85 |
+
|
86 |
+
try:
|
87 |
+
# Process the input
|
88 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
89 |
+
|
90 |
+
# Generate
|
91 |
+
outputs = model.generate(
|
92 |
+
inputs["input_ids"],
|
93 |
+
max_length=max_length,
|
94 |
+
temperature=temperature,
|
95 |
+
do_sample=True,
|
96 |
+
pad_token_id=tokenizer.eos_token_id if hasattr(tokenizer, 'eos_token_id') else tokenizer.pad_token_id,
|
97 |
+
num_return_sequences=1,
|
98 |
+
repetition_penalty=1.2, # Reduce repetition
|
99 |
+
top_k=50,
|
100 |
+
top_p=0.95
|
101 |
+
)
|
102 |
+
|
103 |
+
# Decode and return the generated text
|
104 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
105 |
+
return generated_text
|
106 |
+
except Exception as e:
|
107 |
+
return f"Error during text generation: {str(e)}"
|
108 |
+
|
109 |
+
# Create Gradio interface with better error handling
|
110 |
+
with gr.Blocks(title="Burmese-GPT-v3 Text Generation") as demo:
|
111 |
+
gr.Markdown("# Burmese-GPT-v3 Text Generation")
|
112 |
+
gr.Markdown("Enter a prompt in Burmese to generate text using the Burmese-GPT-v3 model.")
|
113 |
+
|
114 |
+
# Add status indicator
|
115 |
+
with gr.Row():
|
116 |
+
model_status = gr.Markdown("β οΈ Model status: Checking..." if model is None else "β
Model loaded and ready")
|
117 |
+
|
118 |
+
# Model loading button (for manual retry)
|
119 |
+
def load_model_manually():
|
120 |
+
global tokenizer, model
|
121 |
+
try:
|
122 |
+
tokenizer, model = load_model_with_retries(model_name)
|
123 |
+
return "β
Model loaded successfully!"
|
124 |
+
except Exception as e:
|
125 |
+
return f"β Failed to load model: {str(e)}"
|
126 |
+
|
127 |
+
load_button = gr.Button("Retry Loading Model")
|
128 |
+
load_button.click(fn=load_model_manually, outputs=model_status)
|
129 |
+
|
130 |
+
# Add model info
|
131 |
+
gr.Markdown("### Model Information")
|
132 |
+
gr.Markdown("- **Model Name**: WYNN747/Burmese-GPT-v3")
|
133 |
+
gr.Markdown("- **Description**: A language model for Burmese text generation")
|
134 |
+
|
135 |
+
# Input components
|
136 |
+
with gr.Row():
|
137 |
+
with gr.Column(scale=3):
|
138 |
+
prompt = gr.Textbox(
|
139 |
+
lines=5,
|
140 |
+
placeholder="Enter your Burmese text prompt here...",
|
141 |
+
label="Prompt"
|
142 |
+
)
|
143 |
+
with gr.Column(scale=1):
|
144 |
+
max_length = gr.Slider(
|
145 |
+
minimum=50,
|
146 |
+
maximum=500,
|
147 |
+
value=100,
|
148 |
+
step=10,
|
149 |
+
label="Max Length"
|
150 |
+
)
|
151 |
+
temperature = gr.Slider(
|
152 |
+
minimum=0.1,
|
153 |
+
maximum=1.0,
|
154 |
+
value=0.7,
|
155 |
+
step=0.1,
|
156 |
+
label="Temperature"
|
157 |
+
)
|
158 |
+
|
159 |
+
# Generate button
|
160 |
+
generate_btn = gr.Button("Generate Text", variant="primary")
|
161 |
+
|
162 |
+
# Output
|
163 |
+
output = gr.Textbox(lines=10, label="Generated Text")
|
164 |
+
|
165 |
+
# Set up the generation function
|
166 |
+
generate_btn.click(
|
167 |
+
fn=generate_text,
|
168 |
+
inputs=[prompt, max_length, temperature],
|
169 |
+
outputs=output
|
170 |
)
|
171 |
|
172 |
+
# Add examples if available
|
173 |
+
gr.Examples(
|
174 |
+
examples=[
|
175 |
+
["αα―ααΊαα²α· αα»α½ααΊαα±α¬αΊ ααα¬α
αα¬αΈ ααΌα±α¬αααΊαα«αααΊα", 150, 0.7],
|
176 |
+
["ααΌααΊαα¬ααα―ααΊααΆααΎα¬", 200, 0.8],
|
177 |
+
],
|
178 |
+
inputs=[prompt, max_length, temperature],
|
179 |
+
outputs=output,
|
180 |
+
fn=generate_text,
|
181 |
+
cache_examples=True,
|
182 |
+
)
|
183 |
+
|
184 |
+
# Add troubleshooting section
|
185 |
+
gr.Markdown("### Troubleshooting")
|
186 |
+
gr.Markdown("""
|
187 |
+
- If you see timeout errors, try refreshing the page or clicking "Retry Loading Model"
|
188 |
+
- If the model still fails to load, try again later when network conditions improve
|
189 |
+
- Make sure you have a stable internet connection
|
190 |
+
""")
|
191 |
|
192 |
+
# Launch the app with caching and concurrency settings
|
193 |
+
demo.launch(
|
194 |
+
cache_examples=True,
|
195 |
+
show_error=True,
|
196 |
+
server_name="0.0.0.0", # Listen on all network interfaces
|
197 |
+
share=False, # Set to True for temporary public link
|
198 |
+
max_threads=16 # Increase if needed
|
199 |
+
)
|