Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageOps
|
3 |
+
import numpy as np
|
4 |
+
import easyocr
|
5 |
+
from pokedex import (
|
6 |
+
get_base_hp,
|
7 |
+
estimate_level_from_hp,
|
8 |
+
get_english_name,
|
9 |
+
get_pokemon_info,
|
10 |
+
)
|
11 |
+
|
12 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
13 |
+
|
14 |
+
def find_pokemon_from_name_and_hp(name: str, hp: int) -> str:
|
15 |
+
name_en = get_english_name(name)
|
16 |
+
if "❌" in name_en:
|
17 |
+
return "❌ Pokémon not recognized."
|
18 |
+
|
19 |
+
try:
|
20 |
+
base = get_base_hp(name_en)
|
21 |
+
level = estimate_level_from_hp(int(hp), base)
|
22 |
+
if not level:
|
23 |
+
return "⚠️ Could not estimate level with provided HP."
|
24 |
+
info = get_pokemon_info(name_en, level)
|
25 |
+
return f"## 📊 Estimated Level: {level}\n\n{info}"
|
26 |
+
except Exception as e:
|
27 |
+
return f"⚠️ Error: {str(e)}"
|
28 |
+
|
29 |
+
|
30 |
+
def find_pokemon_from_name_and_level(name: str, level: int) -> str:
|
31 |
+
name_en = get_english_name(name)
|
32 |
+
if "❌" in name_en:
|
33 |
+
return "❌ Pokémon not recognized."
|
34 |
+
|
35 |
+
try:
|
36 |
+
info = get_pokemon_info(name_en, level)
|
37 |
+
return f"## 📊 Level: {level}\n\n{info}"
|
38 |
+
except Exception as e:
|
39 |
+
return f"⚠️ Error: {str(e)}"
|
40 |
+
|
41 |
+
|
42 |
+
manual_hp_interface = gr.Interface(
|
43 |
+
fn=find_pokemon_from_name_and_hp,
|
44 |
+
inputs=[
|
45 |
+
gr.Text(label="Pokémon Name (EN or FR)"),
|
46 |
+
gr.Number(label="Observed HP")
|
47 |
+
],
|
48 |
+
outputs="markdown",
|
49 |
+
title="Estimate Level from HP",
|
50 |
+
description="Enter a Pokémon name and its observed HP to estimate its level and view full info."
|
51 |
+
)
|
52 |
+
|
53 |
+
manual_level_interface = gr.Interface(
|
54 |
+
fn=find_pokemon_from_name_and_level,
|
55 |
+
inputs=[
|
56 |
+
gr.Text(label="Pokémon Name (EN or FR)"),
|
57 |
+
gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Level")
|
58 |
+
],
|
59 |
+
outputs="markdown",
|
60 |
+
title="Pokémon Stats at Specific Level",
|
61 |
+
description="Enter a Pokémon name and a level to get its full info sheet."
|
62 |
+
)
|
63 |
+
|
64 |
+
|
65 |
+
# 📌 Mode OCR
|
66 |
+
def find_pokemon_from_card_image(image: Image.Image, source: str) -> str:
|
67 |
+
if image is None:
|
68 |
+
return "❌ No image provided."
|
69 |
+
|
70 |
+
if source == "webcam":
|
71 |
+
image = ImageOps.mirror(image)
|
72 |
+
|
73 |
+
image_np = np.array(image)
|
74 |
+
results = reader.readtext(image_np)
|
75 |
+
|
76 |
+
all_texts = "### 📄 OCR Detected Texts:\n"
|
77 |
+
for i, (bbox, text, conf) in enumerate(results):
|
78 |
+
all_texts += f"- Text #{i+1}: `{text.strip()}` (confidence: {round(conf, 2)})\n"
|
79 |
+
|
80 |
+
card_name = None
|
81 |
+
if len(results) >= 1:
|
82 |
+
text1 = results[0][1].strip().upper()
|
83 |
+
if "IVEA" not in text1:
|
84 |
+
card_name = get_english_name(results[0][1].strip())
|
85 |
+
elif len(results) >= 2:
|
86 |
+
card_name = get_english_name(results[1][1].strip())
|
87 |
+
|
88 |
+
hp = None
|
89 |
+
for (bbox, text, conf) in results[:5]:
|
90 |
+
text_clean = text.strip()
|
91 |
+
if conf > 0.9 and text_clean.isdigit():
|
92 |
+
hp = int(text_clean)
|
93 |
+
break
|
94 |
+
|
95 |
+
level_info = ""
|
96 |
+
pokemon_sheet = ""
|
97 |
+
if hp is not None and card_name and "❌" not in card_name:
|
98 |
+
try:
|
99 |
+
base_hp = get_base_hp(card_name)
|
100 |
+
level = estimate_level_from_hp(hp, base_hp)
|
101 |
+
if level:
|
102 |
+
level_info = f"## 📊 Estimated Level: {level}"
|
103 |
+
pokemon_sheet = get_pokemon_info(card_name, level)
|
104 |
+
else:
|
105 |
+
level_info = "⚠️ Unable to estimate level from provided data."
|
106 |
+
except Exception as e:
|
107 |
+
level_info = f"⚠️ Error while processing: {str(e)}"
|
108 |
+
else:
|
109 |
+
level_info = "⚠️ Not enough information to estimate level."
|
110 |
+
|
111 |
+
return f"""
|
112 |
+
### 🃏 OCR Result
|
113 |
+
**Pokémon Name (EN)**: {card_name or '❓ Not detected'}
|
114 |
+
**HP**: {hp if hp is not None else '❓ Not detected'}
|
115 |
+
{level_info}
|
116 |
+
|
117 |
+
---
|
118 |
+
|
119 |
+
{pokemon_sheet}
|
120 |
+
""".strip()
|
121 |
+
|
122 |
+
ocr_interface = gr.Interface(
|
123 |
+
fn=find_pokemon_from_card_image,
|
124 |
+
inputs=[
|
125 |
+
gr.Image(type="pil", label="Pokémon Card Image", height=300, width=300),
|
126 |
+
gr.Radio(["upload", "webcam"], label="Image Source", value="upload")
|
127 |
+
],
|
128 |
+
outputs="markdown",
|
129 |
+
title="OCR Pokémon Scanner",
|
130 |
+
description="Upload or scan a Pokémon card to automatically extract info and estimate level.",
|
131 |
+
)
|
132 |
+
|
133 |
+
# 🌐 Multi-MCP Tabbed Interface
|
134 |
+
demo = gr.TabbedInterface(
|
135 |
+
interface_list=[manual_hp_interface, manual_level_interface, ocr_interface],
|
136 |
+
tab_names=["Manual from HP", "Manual from level", "Image OCR"]
|
137 |
+
)
|
138 |
+
|
139 |
+
if __name__ == "__main__":
|
140 |
+
demo.launch(mcp_server=True)
|