Update app.py
Browse files
app.py
CHANGED
|
@@ -19,33 +19,39 @@ def load_model():
|
|
| 19 |
repo_id=MODEL_REPO,
|
| 20 |
filename=MODEL_FILE,
|
| 21 |
cache_dir=CACHE_DIR,
|
| 22 |
-
force_download=False
|
| 23 |
)
|
| 24 |
|
| 25 |
return Llama(
|
| 26 |
-
model_path=model_path,
|
| 27 |
-
n_ctx=2048,
|
| 28 |
-
n_threads=4,
|
| 29 |
-
verbose=False
|
| 30 |
)
|
| 31 |
|
| 32 |
# Load model at startup
|
| 33 |
llm = load_model()
|
| 34 |
|
| 35 |
-
# Generation function with
|
| 36 |
def generate_text(prompt, max_tokens=MAX_TOKENS, temp=0.7, top_p=0.95):
|
| 37 |
-
"""Generate text
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
output = llm(
|
| 40 |
prompt=prompt,
|
| 41 |
max_tokens=max_tokens,
|
| 42 |
temperature=temp,
|
| 43 |
top_p=top_p,
|
| 44 |
-
echo=False
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
-
return output["choices"][0]["text"]
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"Error generating text: {str(e)}"
|
| 49 |
|
| 50 |
# UI Components
|
| 51 |
with gr.Blocks(theme="soft") as demo:
|
|
@@ -60,7 +66,8 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 60 |
prompt = gr.Textbox(
|
| 61 |
label="Input Prompt",
|
| 62 |
placeholder="Enter your prompt here...",
|
| 63 |
-
lines=5
|
|
|
|
| 64 |
)
|
| 65 |
max_tokens = gr.Slider(
|
| 66 |
minimum=50,
|
|
@@ -72,14 +79,14 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 72 |
temp = gr.Slider(
|
| 73 |
minimum=0.1,
|
| 74 |
maximum=1.0,
|
| 75 |
-
value=0.
|
| 76 |
step=0.1,
|
| 77 |
label="Creativity (Temperature)"
|
| 78 |
)
|
| 79 |
top_p = gr.Slider(
|
| 80 |
minimum=0.1,
|
| 81 |
maximum=1.0,
|
| 82 |
-
value=0.
|
| 83 |
step=0.05,
|
| 84 |
label="Top-p Sampling"
|
| 85 |
)
|
|
|
|
| 19 |
repo_id=MODEL_REPO,
|
| 20 |
filename=MODEL_FILE,
|
| 21 |
cache_dir=CACHE_DIR,
|
| 22 |
+
force_download=False
|
| 23 |
)
|
| 24 |
|
| 25 |
return Llama(
|
| 26 |
+
model_path=model_path,
|
| 27 |
+
n_ctx=2048,
|
| 28 |
+
n_threads=4,
|
| 29 |
+
verbose=False
|
| 30 |
)
|
| 31 |
|
| 32 |
# Load model at startup
|
| 33 |
llm = load_model()
|
| 34 |
|
| 35 |
+
# Generation function with anti-repetition
|
| 36 |
def generate_text(prompt, max_tokens=MAX_TOKENS, temp=0.7, top_p=0.95):
|
| 37 |
+
"""Generate text with repetition prevention and error handling"""
|
| 38 |
+
if not prompt.strip():
|
| 39 |
+
return "Please enter a valid prompt."
|
| 40 |
+
|
| 41 |
try:
|
| 42 |
output = llm(
|
| 43 |
prompt=prompt,
|
| 44 |
max_tokens=max_tokens,
|
| 45 |
temperature=temp,
|
| 46 |
top_p=top_p,
|
| 47 |
+
echo=False,
|
| 48 |
+
# Anti-repetition parameters
|
| 49 |
+
repeat_penalty=1.2,
|
| 50 |
+
no_repeat_ngram_size=3
|
| 51 |
)
|
| 52 |
+
return output["choices"][0]["text"].strip()
|
| 53 |
except Exception as e:
|
| 54 |
+
return f"⚠️ Error generating text: {str(e)}"
|
| 55 |
|
| 56 |
# UI Components
|
| 57 |
with gr.Blocks(theme="soft") as demo:
|
|
|
|
| 66 |
prompt = gr.Textbox(
|
| 67 |
label="Input Prompt",
|
| 68 |
placeholder="Enter your prompt here...",
|
| 69 |
+
lines=5,
|
| 70 |
+
min_length=10
|
| 71 |
)
|
| 72 |
max_tokens = gr.Slider(
|
| 73 |
minimum=50,
|
|
|
|
| 79 |
temp = gr.Slider(
|
| 80 |
minimum=0.1,
|
| 81 |
maximum=1.0,
|
| 82 |
+
value=0.85,
|
| 83 |
step=0.1,
|
| 84 |
label="Creativity (Temperature)"
|
| 85 |
)
|
| 86 |
top_p = gr.Slider(
|
| 87 |
minimum=0.1,
|
| 88 |
maximum=1.0,
|
| 89 |
+
value=0.9,
|
| 90 |
step=0.05,
|
| 91 |
label="Top-p Sampling"
|
| 92 |
)
|