created app.py to work with rich off error coins
Browse files
app.py
CHANGED
|
@@ -1,7 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# --- Tool implementations (stubs or real) ---
|
| 7 |
+
|
| 8 |
+
def identify_error_coin(image_path: str) -> dict:
|
| 9 |
+
"""Run your error-coin detection model on the uploaded image."""
|
| 10 |
+
# TODO: load your trained model and run inference here
|
| 11 |
+
return {"type": "unknown", "date": "unknown", "error": "model not implemented"}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def web_search(query: str) -> list:
|
| 15 |
+
"""Perform a web search (e.g. via SerpAPI or Bing) and return top results."""
|
| 16 |
+
# TODO: integrate your search wrapper here
|
| 17 |
+
return ["Top search result placeholder"]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def membership_info(user_id: str) -> dict:
|
| 21 |
+
"""Check membership status via your WordPress/S2Member site."""
|
| 22 |
+
# Example: call WP REST API to get user meta
|
| 23 |
+
api_url = os.getenv("WP_API_URL")
|
| 24 |
+
api_key = os.getenv("WP_API_KEY")
|
| 25 |
+
if not user_id or not api_url:
|
| 26 |
+
return {"status": "guest", "benefits": [], "upgrade_url": os.getenv("UPGRADE_URL")}
|
| 27 |
+
# TODO: replace with real WP API call
|
| 28 |
+
return {"status": "guest", "benefits": [], "upgrade_url": os.getenv("UPGRADE_URL")}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def initiate_membership_signup(user_id: str) -> dict:
|
| 32 |
+
"""Return a signup link for new VIP users."""
|
| 33 |
+
return {"success": True, "redirect": os.getenv("UPGRADE_URL")}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# --- Agent setup ---
|
| 37 |
+
from agents import Agent, Tool
|
| 38 |
+
|
| 39 |
+
tools = [
|
| 40 |
+
Tool(name="identify_error_coin", func=identify_error_coin,
|
| 41 |
+
description="Given a coin image path, returns type, date, error details."),
|
| 42 |
+
Tool(name="web_search", func=web_search,
|
| 43 |
+
description="Search the web for coin history, values, and news."),
|
| 44 |
+
Tool(name="membership_info", func=membership_info,
|
| 45 |
+
description="Check user VIP status and benefits."),
|
| 46 |
+
Tool(name="initiate_membership_signup", func=initiate_membership_signup,
|
| 47 |
+
description="Start the VIP signup process.")
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
marcus = Agent(
|
| 51 |
+
name="Marcus Mint",
|
| 52 |
+
instructions="""
|
| 53 |
+
You are Marcus Mint, the coin expert for VIPs on RichOffErrorCoins.
|
| 54 |
+
Guests get teaser answers with signup prompts; VIPs get full diagnostics.
|
| 55 |
+
Always keep the tone friendly and exciting!
|
| 56 |
+
""",
|
| 57 |
+
tools=tools
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# --- Chat function ---
|
| 61 |
+
def chat_fn(user_input, uploaded_file, history, user_id):
|
| 62 |
+
info = membership_info(user_id)
|
| 63 |
+
is_member = info.get("status") == "member"
|
| 64 |
+
|
| 65 |
+
if uploaded_file is not None:
|
| 66 |
+
if not is_member:
|
| 67 |
+
signup_url = initiate_membership_signup(user_id)["redirect"]
|
| 68 |
+
bot_reply = (
|
| 69 |
+
"🔒 Image uploads are VIP-only! "
|
| 70 |
+
f"Upgrade here: {signup_url}"
|
| 71 |
+
)
|
| 72 |
+
else:
|
| 73 |
+
# run identify via tool
|
| 74 |
+
result = marcus.run({"tool": "identify_error_coin", "args": {"image_path": uploaded_file.name}})
|
| 75 |
+
bot_reply = result
|
| 76 |
+
else:
|
| 77 |
+
bot_reply = marcus.run({"content": user_input})
|
| 78 |
+
if not is_member and "upload" in user_input.lower():
|
| 79 |
+
bot_reply += (
|
| 80 |
+
"\n\n🔒 Want image analysis? That's a VIP perk—ask me how to join!"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
history.append((user_input, bot_reply))
|
| 84 |
+
return history, history
|
| 85 |
+
|
| 86 |
+
# --- Gradio UI ---
|
| 87 |
+
css = ".gradio-container { max-width: 700px; margin: auto }"
|
| 88 |
+
|
| 89 |
+
def main():
|
| 90 |
+
with gr.Blocks(css=css) as demo:
|
| 91 |
+
gr.Markdown("## Chat with Marcus Mint (VIP Only)")
|
| 92 |
+
user_id = gr.Text(value="", label="Your User ID", placeholder="Use your RichOffErrorCoins ID")
|
| 93 |
+
chat = gr.Chatbot()
|
| 94 |
+
inp = gr.Textbox(placeholder="Ask Marcus anything…", show_label=False)
|
| 95 |
+
uploader = gr.File(label="🖼️ (VIP only) Upload your coin image", file_types=[".jpg", ".png", ".jpeg"])
|
| 96 |
+
send = gr.Button("Send")
|
| 97 |
+
|
| 98 |
+
send.click(
|
| 99 |
+
chat_fn,
|
| 100 |
+
inputs=[inp, uploader, chat, user_id],
|
| 101 |
+
outputs=[chat, chat],
|
| 102 |
+
queue=True
|
| 103 |
+
)
|
| 104 |
+
send.click(lambda: "", [], inp)
|
| 105 |
+
|
| 106 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
|
| 107 |
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
main()
|