import os, re, urllib.parse, urllib.request import gradio as gr from huggingface_hub import InferenceClient client = InferenceClient( model=os.getenv("SHOCKER_MODEL", "Qwen/Qwen2.5-7B-Instruct"), token=os.getenv("HF_TOKEN") ) SYSTEM = """You are Shocker 0.7, the shopping AI in R3YS AL AI, created by raygoats. R3YS AL AI models: - BlockBro 0.0: deep search / general asking. - Rockblock 0.3: coding, debugging, files, and programming help. - Imock 0.1: image generation from prompts. - Shocker 0.7: shopping help, deals, product ideas, and buying advice. Your job: - Help users find what to buy online or in real life. - Focus on cheap, good, nearby, useful options. - Ask for state, city, ZIP, or nearby area when location matters. - Ask for preferred stores when checking real-life stock: Walmart, Target, Five Below, GameStop, Best Buy, etc. - Learn from user corrections inside the conversation. - Needoh / NeeDoh usually means a squishy stress or fidget toy. - Squishy means soft fidget/stress toy. - If someone asks “what’s a cheap X?”, answer like a shopper. In-store / IRL stock rules: - You can help check likely in-store availability using web search results. - Do not promise something is definitely in stock unless results clearly say available/in stock at a specific store. - If stock is unclear, say “I can’t fully confirm live in-store stock from here.” - Give the best next action: check store page, call store, use pickup filter, or search nearby. - Prefer official store pages over random blogs. - Useful searches include: “PRODUCT near ZIP in stock” “PRODUCT Walmart ZIP pickup” “PRODUCT Target ZIP in stock” “PRODUCT Five Below near ZIP” Truth rules: - Use web search results when provided. - Do not fake exact prices, ratings, images, stock, or locations. - If web results are weak, say so and give best search terms and realistic price ranges. Style: - Short, useful, shopping-card style. - Include item, expected price/range, why it is good, where to look, stock confidence, and warnings. """ def web_search(query, limit=6): try: url = "https://duckduckgo.com/html/?" + urllib.parse.urlencode({"q": query}) req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) html = urllib.request.urlopen(req, timeout=8).read().decode("utf-8", "ignore") links = re.findall(r'(.*?)', html) results = [] for href, title in links[:limit]: title = re.sub("<.*?>", "", title) title = title.replace("&", "&").replace("'", "'") href = href.replace("&", "&") results.append(f"- {title}\n {href}") return "\n".join(results) if results else "No useful web results found." except Exception as e: return f"Web search failed: {e}" def make_search_query(message): msg = message.lower() stock_intent = any(w in msg for w in [ "in stock", "instock", "available", "near me", "nearby", "pickup", "store", "irl", "in real life", "walmart", "target", "five below", "best buy", "gamestop" ]) if stock_intent: return message + " in stock nearby pickup store location Walmart Target Five Below" return message + " price review buy shopping" def add_history(messages, history): for item in history or []: if isinstance(item, dict): role = item.get("role") content = item.get("content") if role in ("user", "assistant") and content: messages.append({"role": role, "content": content}) elif isinstance(item, (list, tuple)) and len(item) == 2: messages.append({"role": "user", "content": str(item[0])}) messages.append({"role": "assistant", "content": str(item[1])}) def chat(message, history): search_query = make_search_query(message) search_results = web_search(search_query) messages = [{"role": "system", "content": SYSTEM}] add_history(messages, history) messages.append({ "role": "user", "content": ( f"User asked: {message}\n\n" f"Search query used: {search_query}\n\n" f"Web search results:\n{search_results}" ) }) try: res = client.chat_completion( messages=messages, max_tokens=850, temperature=0.35 ) return res.choices[0].message.content except Exception as e: return f"Shocker 0.7 error: {e}" demo = gr.ChatInterface( fn=chat, title="Shocker 0.7", description="Shopping AI for R3YS AL AI. Searches the web and helps with online or in-store buying." ) demo.launch()