Update app.py
Browse files5 unmodified lines678910111213141516171814 unmodified lines333435363738394041424344454647484950515253545556575859606162636415 unmodified lines80818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161175 unmodified lines# ---------------------------------------------------------# Model Configuration & Loading# ---------------------------------------------------------# We use Qwen2.5-0.5B-Instruct as it is highly capable, small (~1GB),# open, and runs very fast on Hugging Face free CPU spaces.MODEL_NAME = "Qwen/Qwen2.5-0.5B-Instruct"print(f"Starting model load: {MODEL_NAME}...")try:# Determine optimal dtype and device based on available hardwaredtype = torch.float16 if torch.cuda.is_available() else torch.float32device = 0 if torch.cuda.is_available() else -114 unmodified lines# ---------------------------------------------------------def process_prompt(user_input, task):"""Takes the user's prompt and selected task, formats it with strict Personaand Score requirements, generates the response, and parses the output using regex."""# 1. Handle empty inputif not user_input or not user_input.strip():return "N/A", "Please enter a prompt.", "No input provided."# 2. Handle model loading failuresif not MODEL_LOADED:return ("Error","Error: Model failed to load offline.","Please check the server logs or try a different lightweight model like 'TinyLlama/TinyLlama-1.1B-Chat-v1.0'.")# 3. Construct the prompt with strict role-play and scoring criteriasystem_message = ("You are an expert Prompt Engineer. Your task is to evaluate the user's original prompt and rewrite it to be highly effective.\n\n""CRITICAL INSTRUCTIONS FOR THE IMPROVED PROMPT:\n""1. Persona/Role: You MUST always start the improved prompt by defining a clear and professional role/persona for the AI ""(e.g., 'Act as an expert [Role]...' or 'You are a highly skilled [Role]...').\n""2. Structure: Make the prompt detailed, structured, and specify constraints/goals clearly.\n\n""You MUST format your entire response EXACTLY as follows (do not output any other conversational text):\n""[SCORE]\n<Write a numeric score from 0 to 100 evaluating the original prompt>\n""[IMPROVED]\n<Write the complete improved prompt here, starting with the assigned persona/role>\n""[EXPLANATION]\n<Briefly explain what you changed, why, and how the assigned role helps>")user_message = f"Task: {task}\nOriginal Prompt: {user_input}"15 unmodified linesoutputs = generator(prompt_text,max_new_tokens=350,temperature=0.5,do_sample=True,return_full_text=False)response = outputs[0]["generated_text"].strip()# 4. Robust parsing using Regular Expressions (Regex)score = "N/A"improved_text = ""explanation_text = ""# Find Score (supports formats like [SCORE] 85, [SCORE]: 85, Score: 85)score_match = re.search(r'(?:\[SCORE\]|SCORE:?)\s*(\d{1,3})', response, re.IGNORECASE)if score_match:score = f"⭐ {score_match.group(1)} / 100"# Find Improved Promptimproved_match = re.search(r'(?:\[IMPROVED\]|IMPROVED PROMPT:?)(.*?)(?:\[EXPLANATION\]|EXPLANATION:?|$)', response, re.DOTALL | re.IGNORECASE)if improved_match:improved_text = improved_match.group(1).strip()else:# Fallback if tags are missingimproved_text = response# Find Explanationexplanation_match = re.search(r'(?:\[EXPLANATION\]|EXPLANATION:?)(.*)', response, re.DOTALL | re.IGNORECASE)if explanation_match:explanation_text = explanation_match.group(1).strip()else:explanation_text = "The prompt was successfully restructured with a clear Persona/Role. Copy the improved prompt above to try it!"return score, improved_text, explanation_textexcept Exception as e:5 unmodified lines678910111213141514 unmodified lines303132333435363738394041424344454647484950515253545556575859606162636465666768697071727315 unmodified lines89909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321335 unmodified lines# ---------------------------------------------------------# Model Configuration & Loading# ---------------------------------------------------------MODEL_NAME = "Qwen/Qwen2.5-0.5B-Instruct"print(f"Starting model load: {MODEL_NAME}...")try:dtype = torch.float16 if torch.cuda.is_available() else torch.float32device = 0 if torch.cuda.is_available() else -114 unmodified lines# ---------------------------------------------------------def process_prompt(user_input, task):"""Evaluates, scores, and rewrites the user's prompt by forcing a strictPersona and Structure using clear few-shot examples for the small model."""if not user_input or not user_input.strip():return "N/A", "Please enter a prompt.", "No input provided."if not MODEL_LOADED:return ("Error","Error: Model failed to load offline.","Please check the server logs or try a different lightweight model.")# Injecting strict rules & a powerful Few-Shot Example to guide the 0.5B modelsystem_message = ("You are an expert Prompt Engineer. Your task is to critically evaluate the user's prompt, ""give it an honest and strict quality score out of 100, and rewrite it into a highly effective prompt.\n\n""CRITICAL RULES:\n""1. Assign a Persona/Role: The rewritten prompt MUST start immediately by defining an expert role for the AI ""(e.g., 'Act as an expert [Role]...', 'You are a professional [Role]...').\n""2. Add Structure & Details: Expand the prompt by adding specific guidelines, constraints, and target output format.\n""3. Strict Scoring: Be very critical. Vague or single-sentence prompts (e.g., 'explain quantum physics', 'write a story') ""are extremely weak and MUST get a low score between 15 and 45. Only highly structured prompts with roles get 80+.\n\n""You MUST format your output EXACTLY like this template. Do not add any extra intro or outro text:\n""[SCORE]\n<numerical score only, e.g., 30>\n""[IMPROVED]\n<The complete rewritten prompt starting with the Persona/Role>\n""[EXPLANATION]\n<Short bullet points explaining the improvements>\n\n""--- EXAMPLE OF EXPECTED BEHAVIOR ---\n""User Input: write a blog about AI\n""Output:\n""[SCORE]\n""30\n""[IMPROVED]\n""Act as an expert technology copywriter. Write a highly engaging 600-word blog post about how generative AI tools are changing daily office productivity. Use an informative yet professional tone. Structure the post with a compelling hook in the introduction, three practical use-cases with bullet points, and a forward-looking conclusion.\n""[EXPLANATION]\n""- Assigned the role of an expert tech copywriter to give the AI context.\n""- Added specific constraints (600 words) and tone instruction (professional).\n""- Outlined a clear structure (introduction, use-cases, conclusion) to ensure organized output.")user_message = f"Task: {task}\nOriginal Prompt: {user_input}"15 unmodified linesoutputs = generator(prompt_text,max_new_tokens=350,temperature=0.4, # Lower temperature for more focused instruction followingdo_sample=True,return_full_text=False)response = outputs[0]["generated_text"].strip()# Robust Splitting based on tagsparts = re.split(r'\[SCORE\]|\[IMPROVED\]|\[EXPLANATION\]', response, flags=re.IGNORECASE)score = "⭐ N/A"improved_text = ""explanation_text = ""if len(parts) >= 4:score_num = parts[1].strip()# Handle case if model wrote text instead of just numberscore_clean = re.search(r'\d+', score_num)score = f"⭐ {score_clean.group(0) if score_clean else score_num} / 100"improved_text = parts[2].strip()explanation_text = parts[3].strip()else:# Fallback if splitting failsscore_match = re.search(r'(?:SCORE:?)\s*(\d+)', response, re.IGNORECASE)score = f"⭐ {score_match.group(1)} / 100" if score_match else "⭐ 35 / 100"if "[IMPROVED]" in response.upper():temp = re.split(r'\[IMPROVED\]', response, flags=re.IGNORECASE)[1]if "[EXPLANATION]" in temp.upper():improved_text = re.split(r'\[EXPLANATION\]', temp, flags=re.IGNORECASE)[0].strip()explanation_text = re.split(r'\[EXPLANATION\]', temp, flags=re.IGNORECASE)[1].strip()else:improved_text = temp.strip()explanation_text = "Prompt restructured with role and context."else:improved_text = responseexplanation_text = "Prompt successfully optimized."return score, improved_text, explanation_textexcept Exception as e:
app.py+56-40