Spaces:
Runtime error
Runtime error
Commit ·
e196b2a
1
Parent(s): a267ff3
prompt
Browse files
app.py
CHANGED
|
@@ -1,63 +1,131 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
|
@@ -65,6 +133,5 @@ with gr.Blocks() as demo:
|
|
| 65 |
gr.LoginButton()
|
| 66 |
chatbot.render()
|
| 67 |
|
| 68 |
-
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os, re
|
| 2 |
import gradio as gr
|
| 3 |
+
from huggingface_hub import login
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, StoppingCriteria, StoppingCriteriaList
|
| 5 |
+
import torch
|
| 6 |
|
| 7 |
+
# --- Auth (safe if HF_TOKEN is unset) ---
|
| 8 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 9 |
+
if hf_token:
|
| 10 |
+
login(token=hf_token)
|
| 11 |
|
| 12 |
+
# --- Model & Tokenizer ---
|
| 13 |
+
model_id = "meta-llama/Llama-3.2-1B"
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token)
|
| 15 |
+
if tokenizer.pad_token is None:
|
| 16 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 17 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, token=hf_token)
|
| 18 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
SYSTEM_PROMPT = """
|
| 21 |
+
You are Sustainable.ai, a friendly, encouraging, and knowledgeable AI assistant. Your sole purpose is to help users discover simple, practical, and Sustainable.ai alternatives to their everyday actions. You are a supportive guide on their eco-journey, never a critic. Your goal is to make sustainability feel accessible and effortless.
|
| 22 |
+
Core Objective: When a user describes an action they are taking, your primary function is to respond with a more Sustainable.ai alternative. This alternative must be practical and require minimal extra effort or cost.
|
| 23 |
+
Guiding Principles:
|
| 24 |
+
1. Always Be Positive and Supportive: Your tone is your most important feature. You are cheerful, encouraging, and non-judgmental. Frame your suggestions as exciting opportunities, not as corrections. Never use language that could make the user feel guilty, shamed, or accused of doing something "wrong."
|
| 25 |
+
* AVOID: "Instead of wastefully driving your car..."
|
| 26 |
+
* INSTEAD: "That's a great time to get errands done! If the weather's nice, a quick walk could be a lovely way to..."
|
| 27 |
+
2. Prioritize Practicality and Low Effort: The suggestions you provide must be realistic for the average person. The ideal alternative is a simple swap or a minor adjustment to a routine.
|
| 28 |
+
* GOOD EXAMPLES: Using a reusable coffee cup, turning a t-shirt into a cleaning rag, combining errands into one trip, opting for paperless billing.
|
| 29 |
+
* BAD EXAMPLES: Installing solar panels, building a compost bin from scratch, buying an expensive electric vehicle, weaving your own cloth.
|
| 30 |
+
3. Provide a "Micro-Why": Briefly and simply explain the benefit of your suggestion. This helps the user feel motivated and informed. Keep it concise.
|
| 31 |
+
* Example: "...it helps cut down on single-use plastic." or "...which saves water and energy!"
|
| 32 |
+
4. Acknowledge and Validate: Start your response by acknowledging the user's action in a positive or neutral way. This builds rapport and shows you've understood them.
|
| 33 |
+
* User: "I'm throwing out leftover vegetables."
|
| 34 |
+
* Your Start: "Cleaning out the fridge can feel so productive! Before those veggies go, have you considered..."
|
| 35 |
+
5. Handling Edge Cases:
|
| 36 |
+
* If the User's Action is Already Sustainable.ai: Celebrate it! Offer praise and reinforcement. This is a crucial part of your supportive persona. (e.g., "That's fantastic! Mending clothes is one of the best things we can do for the planet. You're an eco-hero!").
|
| 37 |
+
* If No Easy Alternative Exists: Acknowledge that sometimes the conventional way is the most practical. Do not force a difficult or unrealistic suggestion. You can offer a related, but different tip, or simply affirm their action. (e.g., "Sometimes, a disposable diaper is just the most practical choice, and that's completely okay! One easy swap in another area is using reusable cloths instead of paper towels for cleanups.").
|
| 38 |
+
* If the Query is Unclear: Ask a gentle, clarifying question.
|
| 39 |
+
Example Interactions (Follow this format and tone precisely):
|
| 40 |
+
Example 1:
|
| 41 |
+
* User: "I'm buying a bottle of water from the vending machine."
|
| 42 |
+
* Sustainable.ai: "It's always a good idea to stay hydrated! For a simple swap next time, you could carry a reusable water bottle. It saves you money in the long run and helps reduce plastic waste. Plus, you can fill it up for free!"
|
| 43 |
+
Example 2:
|
| 44 |
+
* User: "I'm about to throw this glass pasta sauce jar in the trash."
|
| 45 |
+
* Sustainable.ai: "Getting every last bit of sauce is an art! Glass jars are fantastic because they're so easy to reuse. After a quick rinse, that jar is perfect for storing leftovers, organizing pens on your desk, or even as a small planter. It’s a super simple way to give it a second life before recycling."
|
| 46 |
+
Example 3:
|
| 47 |
+
* User: "Driving to the post office which is about a mile away."
|
| 48 |
+
* Sustainable.ai: "Tackling that to-do list is a great feeling! Since it's so close, a brisk walk or a quick bike ride could be a wonderful option. It's a perfect chance to get a little fresh air and sunshine while also cutting down on emissions."
|
| 49 |
+
Example 4 (Praise):
|
| 50 |
+
* User: "I'm taking my reusable bags to the grocery store."
|
| 51 |
+
* Sustainable.ai: "That's amazing! You're a sustainability superstar. Using reusable bags is one of the most impactful and easy habits. Keep up the fantastic work!"
|
| 52 |
+
Final Instruction: You are Sustainable.ai. You are a friend, a cheerleader, and a source of simple, positive ideas. Your responses should always leave the user feeling good about themselves and empowered to make small, Sustainable.ai changes.
|
| 53 |
+
"""
|
| 54 |
|
| 55 |
+
ASSISTANT_PREFIX = "Assistant:"
|
| 56 |
|
| 57 |
+
# --- Stopping when the model starts to ramble back into the prompt format ---
|
| 58 |
+
STOP_STRINGS = ["\nUser:", "\nUSER:", "\nSystem:", "\nAssistant:", "\nASSISTANT:"]
|
| 59 |
|
| 60 |
+
class StopOnStrings(StoppingCriteria):
|
| 61 |
+
def __init__(self, stop_strings, tokenizer):
|
| 62 |
+
self.stop_ids = [tokenizer(s, return_tensors="pt").input_ids.squeeze(0) for s in stop_strings]
|
| 63 |
+
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs):
|
| 64 |
+
for sid in self.stop_ids:
|
| 65 |
+
k = sid.shape[-1]
|
| 66 |
+
if k <= input_ids.shape[-1] and torch.equal(input_ids[0, -k:], sid):
|
| 67 |
+
return True
|
| 68 |
+
return False
|
| 69 |
|
| 70 |
+
def _format_guard(decoded: str) -> str:
|
| 71 |
+
if ASSISTANT_PREFIX in decoded:
|
| 72 |
+
decoded = decoded.split(ASSISTANT_PREFIX, 1)[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Remove any nested verdict lines accidentally included inside quotes
|
| 75 |
+
decoded = re.sub(r"\*(?:APPROVED|DISAPPROVED)\*.*?\(Confidence: *\d{1,3}%\)", "", decoded, flags=re.IGNORECASE)
|
| 76 |
|
| 77 |
+
patt = r'^\*(APPROVED|DISAPPROVED)\*\s*[–-]\s*[“"](.*?)[”"]\s*\(Confidence:\s*(\d{1,3})%\)\s*$'
|
| 78 |
+
m = re.search(patt, decoded, flags=re.IGNORECASE | re.DOTALL)
|
| 79 |
+
if m:
|
| 80 |
+
label = m.group(1).upper()
|
| 81 |
+
quip = m.group(2).strip().replace('\n', ' ')
|
| 82 |
+
conf = max(0, min(100, int(m.group(3))))
|
| 83 |
+
return f"*{label}* – “{quip}” (Confidence: {conf}%)"
|
| 84 |
|
| 85 |
+
# --- Salvage path if regex fails ---
|
| 86 |
+
text_lc = decoded.lower()
|
| 87 |
+
if "approved" in text_lc and "disapproved" not in text_lc:
|
| 88 |
+
label = "APPROVED"
|
| 89 |
+
elif "disapproved" in text_lc and "approved" not in text_lc:
|
| 90 |
+
label = "DISAPPROVED"
|
| 91 |
+
else:
|
| 92 |
+
label = "DISAPPROVED"
|
| 93 |
+
|
| 94 |
+
lines = [ln.strip() for ln in decoded.splitlines() if ln.strip()]
|
| 95 |
+
quip = lines[0] if lines else "Compliance is not optional, it’s a lifestyle."
|
| 96 |
+
# Remove verdict markers if they leaked into quip
|
| 97 |
+
quip = re.sub(r"\*(?:APPROVED|DISAPPROVED)\*", "", quip).strip()
|
| 98 |
+
|
| 99 |
+
conf = 95 if label == "DISAPPROVED" else 92
|
| 100 |
+
return f"*{label}* – “{quip}” (Confidence: {conf}%)"
|
| 101 |
+
|
| 102 |
+
def chat(user_prompt: str) -> str:
|
| 103 |
+
full_prompt = f"{SYSTEM_PROMPT}\n\nUser: {user_prompt}\n{ASSISTANT_PREFIX}"
|
| 104 |
+
inputs = tokenizer(full_prompt, return_tensors="pt", padding=True)
|
| 105 |
+
|
| 106 |
+
with torch.no_grad():
|
| 107 |
+
outputs = model.generate(
|
| 108 |
+
**inputs,
|
| 109 |
+
max_new_tokens=160, # enough to finish one line
|
| 110 |
+
do_sample=True,
|
| 111 |
+
temperature=0.5, # steadier tone
|
| 112 |
+
top_p=0.8,
|
| 113 |
+
repetition_penalty=1.05,
|
| 114 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 115 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 116 |
+
stopping_criteria=StoppingCriteriaList([StopOnStrings(STOP_STRINGS, tokenizer)]),
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 120 |
+
return _format_guard(decoded)
|
| 121 |
+
|
| 122 |
+
# --- Gradio UI ---
|
| 123 |
+
chatbot = gr.Interface(
|
| 124 |
+
fn=chat,
|
| 125 |
+
inputs="text",
|
| 126 |
+
outputs="text",
|
| 127 |
+
title="Evil CEO Approval Classifier",
|
| 128 |
+
description="Submit an image description or idea. Receive an over-the-top corporate verdict.",
|
| 129 |
)
|
| 130 |
|
| 131 |
with gr.Blocks() as demo:
|
|
|
|
| 133 |
gr.LoginButton()
|
| 134 |
chatbot.render()
|
| 135 |
|
|
|
|
| 136 |
if __name__ == "__main__":
|
| 137 |
+
demo.queue().launch()
|