Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -13,10 +13,8 @@ current_model_id = None
|
|
| 13 |
model = None
|
| 14 |
processor = None
|
| 15 |
|
| 16 |
-
# SAFELY LOADING TOKEN FROM SPACE SECRETS (No hardcoding)
|
| 17 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 18 |
|
| 19 |
-
# --- MODEL LOADER ---
|
| 20 |
def load_selected_model(repo_id):
|
| 21 |
global model, processor, current_model_id
|
| 22 |
if repo_id == current_model_id and model is not None:
|
|
@@ -43,7 +41,6 @@ def load_selected_model(repo_id):
|
|
| 43 |
except Exception as e:
|
| 44 |
return f"❌ Error loading {repo_id}: {str(e)}"
|
| 45 |
|
| 46 |
-
# --- PARSER ---
|
| 47 |
def extract_tag(tag, text):
|
| 48 |
match = re.search(f"<(?:{tag})?>(.*?)</(?:{tag})?", text, re.IGNORECASE)
|
| 49 |
if not match:
|
|
@@ -86,18 +83,11 @@ def run_qwen(image, prompt_text, max_tokens=150):
|
|
| 86 |
except Exception as e:
|
| 87 |
return f"Extraction Failed: {str(e)}"
|
| 88 |
|
| 89 |
-
# --- TAB FUNCTIONS ---
|
| 90 |
def ocr_extraction(front_img):
|
| 91 |
prompt = "Extract details inside these XML tags ONLY:\n<ID></ID>\n<NAME></NAME>\n<DOB></DOB>\n<NAT></NAT>"
|
| 92 |
raw_output = run_qwen(front_img, prompt, max_tokens=150)
|
| 93 |
return build_enterprise_json(raw_output)
|
| 94 |
|
| 95 |
-
def chat_with_doc(image, user_message, chat_history):
|
| 96 |
-
ai_response = run_qwen(image, user_message, max_tokens=200)
|
| 97 |
-
chat_history.append((user_message, ai_response))
|
| 98 |
-
return "", chat_history
|
| 99 |
-
|
| 100 |
-
# --- GRADIO UI ---
|
| 101 |
with gr.Blocks() as demo:
|
| 102 |
gr.Markdown("# 🪪 CSM Universal Model Testing Playground")
|
| 103 |
|
|
@@ -132,17 +122,16 @@ with gr.Blocks() as demo:
|
|
| 132 |
with gr.Column(scale=1):
|
| 133 |
chat_img_input = gr.Image(type="pil", label="Document Attachment")
|
| 134 |
with gr.Column(scale=2):
|
| 135 |
-
|
|
|
|
| 136 |
with gr.Row():
|
| 137 |
chat_input = gr.Textbox(placeholder="Ask anything...", show_label=False)
|
| 138 |
send_btn = gr.Button("Send")
|
| 139 |
|
|
|
|
| 140 |
def chat_wrapper(image, user_message, chat_history):
|
| 141 |
-
if not chat_history:
|
| 142 |
-
chat_history = []
|
| 143 |
ai_response = run_qwen(image, user_message, max_tokens=200)
|
| 144 |
-
chat_history.append(
|
| 145 |
-
chat_history.append({"role": "assistant", "content": ai_response})
|
| 146 |
return "", chat_history
|
| 147 |
|
| 148 |
send_btn.click(chat_wrapper, inputs=[chat_img_input, chat_input, chatbot], outputs=[chat_input, chatbot])
|
|
|
|
| 13 |
model = None
|
| 14 |
processor = None
|
| 15 |
|
|
|
|
| 16 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 17 |
|
|
|
|
| 18 |
def load_selected_model(repo_id):
|
| 19 |
global model, processor, current_model_id
|
| 20 |
if repo_id == current_model_id and model is not None:
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
return f"❌ Error loading {repo_id}: {str(e)}"
|
| 43 |
|
|
|
|
| 44 |
def extract_tag(tag, text):
|
| 45 |
match = re.search(f"<(?:{tag})?>(.*?)</(?:{tag})?", text, re.IGNORECASE)
|
| 46 |
if not match:
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
return f"Extraction Failed: {str(e)}"
|
| 85 |
|
|
|
|
| 86 |
def ocr_extraction(front_img):
|
| 87 |
prompt = "Extract details inside these XML tags ONLY:\n<ID></ID>\n<NAME></NAME>\n<DOB></DOB>\n<NAT></NAT>"
|
| 88 |
raw_output = run_qwen(front_img, prompt, max_tokens=150)
|
| 89 |
return build_enterprise_json(raw_output)
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
with gr.Blocks() as demo:
|
| 92 |
gr.Markdown("# 🪪 CSM Universal Model Testing Playground")
|
| 93 |
|
|
|
|
| 122 |
with gr.Column(scale=1):
|
| 123 |
chat_img_input = gr.Image(type="pil", label="Document Attachment")
|
| 124 |
with gr.Column(scale=2):
|
| 125 |
+
# REMOVED: type="messages" to make it compatible with all Gradio versions
|
| 126 |
+
chatbot = gr.Chatbot(label="Chat Interface", height=400)
|
| 127 |
with gr.Row():
|
| 128 |
chat_input = gr.Textbox(placeholder="Ask anything...", show_label=False)
|
| 129 |
send_btn = gr.Button("Send")
|
| 130 |
|
| 131 |
+
# REVERTED back to standard list of tuples format
|
| 132 |
def chat_wrapper(image, user_message, chat_history):
|
|
|
|
|
|
|
| 133 |
ai_response = run_qwen(image, user_message, max_tokens=200)
|
| 134 |
+
chat_history.append((user_message, ai_response))
|
|
|
|
| 135 |
return "", chat_history
|
| 136 |
|
| 137 |
send_btn.click(chat_wrapper, inputs=[chat_img_input, chat_input, chatbot], outputs=[chat_input, chatbot])
|