Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
| 4 |
import warnings
|
| 5 |
|
| 6 |
# Suppress warnings
|
|
@@ -11,42 +12,23 @@ model_loaded = False
|
|
| 11 |
tokenizer = None
|
| 12 |
model = None
|
| 13 |
|
| 14 |
-
# Try
|
| 15 |
try:
|
| 16 |
-
print("Loading
|
| 17 |
-
model_name = "
|
| 18 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name
|
| 19 |
-
model =
|
| 20 |
-
model_name,
|
| 21 |
-
torch_dtype=torch.bfloat16,
|
| 22 |
-
device_map="auto",
|
| 23 |
-
trust_remote_code=True
|
| 24 |
-
)
|
| 25 |
model_loaded = True
|
| 26 |
-
print("
|
| 27 |
except Exception as e:
|
| 28 |
-
print(f"
|
| 29 |
-
|
| 30 |
-
# Try FLAN-T5 model (seq2seq)
|
| 31 |
-
try:
|
| 32 |
-
print("Loading FLAN-T5 model...")
|
| 33 |
-
model_name = "google/flan-t5-base"
|
| 34 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 35 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 36 |
-
model_loaded = True
|
| 37 |
-
print("FLAN-T5 model loaded successfully")
|
| 38 |
-
except Exception as e2:
|
| 39 |
-
print(f"FLAN-T5 model also failed: {e2}")
|
| 40 |
-
|
| 41 |
-
# Final fallback - use a simple template system
|
| 42 |
-
print("Using template-based generation")
|
| 43 |
-
model_loaded = False
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
"""
|
| 47 |
if not model_loaded or not tokenizer or not model:
|
| 48 |
-
# Template-based fallback
|
| 49 |
-
|
| 50 |
<html>
|
| 51 |
<head>
|
| 52 |
<title>{prompt or 'AI Generated Website'}</title>
|
|
@@ -65,39 +47,42 @@ def generate_code(prompt):
|
|
| 65 |
</div>
|
| 66 |
</body>
|
| 67 |
</html>"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
try:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
-
# Fallback
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
</head>
|
| 95 |
-
<body>
|
| 96 |
-
<h1>Generation Error</h1>
|
| 97 |
-
<p>{str(e)}</p>
|
| 98 |
-
<p>Using template instead...</p>
|
| 99 |
-
</body>
|
| 100 |
-
</html>"""
|
| 101 |
|
| 102 |
def improve_code(description, current_code):
|
| 103 |
"""Improve existing code"""
|
|
@@ -105,29 +90,16 @@ def improve_code(description, current_code):
|
|
| 105 |
return current_code
|
| 106 |
|
| 107 |
try:
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
outputs = model.generate(**inputs, max_new_tokens=600, temperature=0.7)
|
| 113 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 114 |
-
else:
|
| 115 |
-
# ERNIE handling
|
| 116 |
-
prompt = f"Improve this HTML code based on: {description}\n\nCurrent code:\n{current_code}\n\nReturn only the improved HTML code."
|
| 117 |
-
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to("cuda")
|
| 118 |
-
outputs = model.generate(**inputs, max_new_tokens=800, temperature=0.7, do_sample=True)
|
| 119 |
-
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 120 |
-
result = result[len(prompt):]
|
| 121 |
-
if '<!DOCTYPE html>' in result:
|
| 122 |
-
start = result.find('<!DOCTYPE html>')
|
| 123 |
-
return result[start:]
|
| 124 |
-
return result
|
| 125 |
except Exception as e:
|
| 126 |
return current_code
|
| 127 |
|
| 128 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 129 |
gr.Markdown("# AI Website Builder")
|
| 130 |
-
gr.Markdown("Powered by local AI models")
|
| 131 |
|
| 132 |
with gr.Tab("Builder"):
|
| 133 |
with gr.Row():
|
|
@@ -154,9 +126,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 154 |
with gr.Row():
|
| 155 |
preview = gr.HTML(label="Website Preview")
|
| 156 |
|
| 157 |
-
# Event handling
|
| 158 |
gen_btn.click(
|
| 159 |
-
fn=
|
| 160 |
inputs=desc_input,
|
| 161 |
outputs=code_editor
|
| 162 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
+
import time
|
| 5 |
import warnings
|
| 6 |
|
| 7 |
# Suppress warnings
|
|
|
|
| 12 |
tokenizer = None
|
| 13 |
model = None
|
| 14 |
|
| 15 |
+
# Try FLAN-T5 model (more reliable)
|
| 16 |
try:
|
| 17 |
+
print("Loading FLAN-T5 model...")
|
| 18 |
+
model_name = "google/flan-t5-base"
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
model_loaded = True
|
| 22 |
+
print("FLAN-T5 model loaded successfully")
|
| 23 |
except Exception as e:
|
| 24 |
+
print(f"FLAN-T5 model failed: {e}")
|
| 25 |
+
model_loaded = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
def stream_code(prompt):
|
| 28 |
+
"""Stream HTML code generation token by token"""
|
| 29 |
if not model_loaded or not tokenizer or not model:
|
| 30 |
+
# Template-based fallback with streaming effect
|
| 31 |
+
template = f"""<!DOCTYPE html>
|
| 32 |
<html>
|
| 33 |
<head>
|
| 34 |
<title>{prompt or 'AI Generated Website'}</title>
|
|
|
|
| 47 |
</div>
|
| 48 |
</body>
|
| 49 |
</html>"""
|
| 50 |
+
|
| 51 |
+
# Stream character by character
|
| 52 |
+
for i in range(len(template)):
|
| 53 |
+
time.sleep(0.01) # Fast streaming
|
| 54 |
+
yield template[:i+1]
|
| 55 |
+
return
|
| 56 |
|
| 57 |
try:
|
| 58 |
+
full_prompt = f"Create a complete HTML file with CSS and JavaScript for: {prompt}. Return only valid HTML code."
|
| 59 |
+
inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 60 |
+
|
| 61 |
+
# Generate with streaming
|
| 62 |
+
outputs = model.generate(
|
| 63 |
+
**inputs,
|
| 64 |
+
max_new_tokens=88000,
|
| 65 |
+
temperature=0.7,
|
| 66 |
+
do_sample=True,
|
| 67 |
+
output_scores=True,
|
| 68 |
+
return_dict_in_generate=True
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Decode and stream token by token
|
| 72 |
+
generated_tokens = outputs.sequences[0]
|
| 73 |
+
decoded = tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
| 74 |
+
|
| 75 |
+
# Stream character by character for smooth effect
|
| 76 |
+
for i in range(len(decoded)):
|
| 77 |
+
time.sleep(0.005) # Very fast streaming
|
| 78 |
+
yield decoded[:i+1]
|
| 79 |
+
|
| 80 |
except Exception as e:
|
| 81 |
+
# Fallback streaming
|
| 82 |
+
error_template = f"<!-- Error occurred: {str(e)} -->\n<!DOCTYPE html>\n<html>\n<head>\n <title>Error</title>\n</head>\n<body>\n <h1>Generation Failed</h1>\n</body>\n</html>"
|
| 83 |
+
for i in range(len(error_template)):
|
| 84 |
+
time.sleep(0.01)
|
| 85 |
+
yield error_template[:i+1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
def improve_code(description, current_code):
|
| 88 |
"""Improve existing code"""
|
|
|
|
| 90 |
return current_code
|
| 91 |
|
| 92 |
try:
|
| 93 |
+
prompt = f"Improve this HTML code based on the request: {description}\n\nCurrent code:\n{current_code}"
|
| 94 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 95 |
+
outputs = model.generate(**inputs, max_new_tokens=600, temperature=0.7)
|
| 96 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
return current_code
|
| 99 |
|
| 100 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 101 |
gr.Markdown("# AI Website Builder")
|
| 102 |
+
gr.Markdown("Powered by local AI models - Streaming Generation")
|
| 103 |
|
| 104 |
with gr.Tab("Builder"):
|
| 105 |
with gr.Row():
|
|
|
|
| 126 |
with gr.Row():
|
| 127 |
preview = gr.HTML(label="Website Preview")
|
| 128 |
|
| 129 |
+
# Event handling with streaming
|
| 130 |
gen_btn.click(
|
| 131 |
+
fn=stream_code,
|
| 132 |
inputs=desc_input,
|
| 133 |
outputs=code_editor
|
| 134 |
)
|