Spaces:
Sleeping
Sleeping
new model
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
"""
|
| 2 |
=========================================================
|
| 3 |
-
app.py — Green Greta (Gradio +
|
| 4 |
-
-
|
| 5 |
-
- LLM:
|
| 6 |
-
- LangChain v0.2
|
| 7 |
- Robust JSON parsing for schema-shaped output
|
| 8 |
- EfficientNet input size fix (224x224)
|
| 9 |
- Gradio binds to 0.0.0.0:7860 (Docker-friendly)
|
|
@@ -33,17 +33,20 @@ from fake_useragent import UserAgent
|
|
| 33 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 34 |
from langchain_core.prompts import ChatPromptTemplate
|
| 35 |
from langchain_core.output_parsers import PydanticOutputParser
|
| 36 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 37 |
from langchain_community.document_loaders import WebBaseLoader
|
| 38 |
-
from langchain_community.llms import HuggingFaceEndpoint
|
| 39 |
from langchain_community.vectorstores import Chroma
|
| 40 |
from langchain.chains import ConversationalRetrievalChain
|
| 41 |
from langchain.memory import ConversationBufferMemory
|
| 42 |
|
| 43 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
from pydantic.v1 import BaseModel, Field
|
| 45 |
|
| 46 |
-
# Hugging Face Hub
|
| 47 |
from huggingface_hub import snapshot_download
|
| 48 |
|
| 49 |
# Local theming + URLs list
|
|
@@ -77,18 +80,17 @@ class_labels = ["cardboard", "glass", "metal", "paper", "plastic", "trash"]
|
|
| 77 |
def predict_image(input_image: Image.Image):
|
| 78 |
"""
|
| 79 |
Resize the user-uploaded image and preprocess it for EfficientNetB0.
|
| 80 |
-
Works with a TFSMLayer (SavedModel) that
|
| 81 |
"""
|
| 82 |
img = input_image.convert("RGB").resize((224, 224)) # EfficientNetB0 expects 224x224
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
-
outputs = model1(image_array)
|
| 89 |
if isinstance(outputs, dict) and outputs:
|
| 90 |
-
|
| 91 |
-
preds = outputs[
|
| 92 |
else:
|
| 93 |
preds = outputs
|
| 94 |
|
|
@@ -188,33 +190,26 @@ qa_prompt = ChatPromptTemplate.from_template(
|
|
| 188 |
|
| 189 |
|
| 190 |
# =============================
|
| 191 |
-
# 4) LLM (
|
| 192 |
# =============================
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
)
|
| 205 |
|
| 206 |
-
|
| 207 |
-
# MIXTRAL_ENDPOINT_URL = os.environ.get("HF_ENDPOINT_URL")
|
| 208 |
-
# if MIXTRAL_ENDPOINT_URL:
|
| 209 |
-
# llm = HuggingFaceEndpoint(
|
| 210 |
-
# endpoint_url=MIXTRAL_ENDPOINT_URL,
|
| 211 |
-
# task="text-generation",
|
| 212 |
-
# max_new_tokens=1024,
|
| 213 |
-
# temperature=0.2,
|
| 214 |
-
# top_k=50,
|
| 215 |
-
# repetition_penalty=1.05,
|
| 216 |
-
# do_sample=True,
|
| 217 |
-
# )
|
| 218 |
|
| 219 |
|
| 220 |
# ===========================================
|
|
|
|
| 1 |
"""
|
| 2 |
=========================================================
|
| 3 |
+
app.py — Green Greta (Gradio + TF/Keras 3 + LangChain v0.2)
|
| 4 |
+
- Image model: load TF SavedModel via keras.layers.TFSMLayer (Keras 3 safe)
|
| 5 |
+
- LLM: local transformers pipeline (no HF API token required)
|
| 6 |
+
- LangChain v0.2 imports (text_splitters/core/community)
|
| 7 |
- Robust JSON parsing for schema-shaped output
|
| 8 |
- EfficientNet input size fix (224x224)
|
| 9 |
- Gradio binds to 0.0.0.0:7860 (Docker-friendly)
|
|
|
|
| 33 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 34 |
from langchain_core.prompts import ChatPromptTemplate
|
| 35 |
from langchain_core.output_parsers import PydanticOutputParser
|
|
|
|
| 36 |
from langchain_community.document_loaders import WebBaseLoader
|
|
|
|
| 37 |
from langchain_community.vectorstores import Chroma
|
| 38 |
from langchain.chains import ConversationalRetrievalChain
|
| 39 |
from langchain.memory import ConversationBufferMemory
|
| 40 |
|
| 41 |
+
# Embeddings (community version works; you can switch to langchain-huggingface later)
|
| 42 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 43 |
+
# If you prefer to silence deprecation warnings in the future:
|
| 44 |
+
# from langchain_huggingface import HuggingFaceEmbeddings # pip install -U langchain-huggingface
|
| 45 |
+
|
| 46 |
+
# Pydantic for schema in prompt
|
| 47 |
from pydantic.v1 import BaseModel, Field
|
| 48 |
|
| 49 |
+
# Hugging Face Hub helper for SavedModel
|
| 50 |
from huggingface_hub import snapshot_download
|
| 51 |
|
| 52 |
# Local theming + URLs list
|
|
|
|
| 80 |
def predict_image(input_image: Image.Image):
|
| 81 |
"""
|
| 82 |
Resize the user-uploaded image and preprocess it for EfficientNetB0.
|
| 83 |
+
Works with a TFSMLayer (SavedModel) that may return a dict of tensors.
|
| 84 |
"""
|
| 85 |
img = input_image.convert("RGB").resize((224, 224)) # EfficientNetB0 expects 224x224
|
| 86 |
+
x = tf.keras.preprocessing.image.img_to_array(img)
|
| 87 |
+
x = tf.keras.applications.efficientnet.preprocess_input(x)
|
| 88 |
+
x = tf.expand_dims(x, 0) # [1, 224, 224, 3]
|
| 89 |
|
| 90 |
+
outputs = model1(x)
|
|
|
|
| 91 |
if isinstance(outputs, dict) and outputs:
|
| 92 |
+
key = next(iter(outputs))
|
| 93 |
+
preds = outputs[key]
|
| 94 |
else:
|
| 95 |
preds = outputs
|
| 96 |
|
|
|
|
| 190 |
|
| 191 |
|
| 192 |
# =============================
|
| 193 |
+
# 4) LLM (token-free local model)
|
| 194 |
# =============================
|
| 195 |
+
# Avoids HF Endpoint auth + deprecated .post path. Good defaults for CPU.
|
| 196 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 197 |
+
from langchain_community.llms import HuggingFacePipeline
|
| 198 |
+
|
| 199 |
+
LOCAL_MODEL_ID = os.environ.get("LOCAL_LLM", "google/flan-t5-base")
|
| 200 |
+
|
| 201 |
+
tok = AutoTokenizer.from_pretrained(LOCAL_MODEL_ID)
|
| 202 |
+
mdl = AutoModelForSeq2SeqLM.from_pretrained(LOCAL_MODEL_ID)
|
| 203 |
+
|
| 204 |
+
gen = pipeline(
|
| 205 |
+
task="text2text-generation",
|
| 206 |
+
model=mdl,
|
| 207 |
+
tokenizer=tok,
|
| 208 |
+
max_new_tokens=512,
|
| 209 |
+
do_sample=False,
|
| 210 |
)
|
| 211 |
|
| 212 |
+
llm = HuggingFacePipeline(pipeline=gen)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
|
| 215 |
# ===========================================
|