ALVHB95 commited on
Commit
2b15ba2
·
1 Parent(s): 0b7cbe9
Files changed (1) hide show
  1. app.py +35 -40
app.py CHANGED
@@ -1,9 +1,9 @@
1
  """
2
  =========================================================
3
- app.py — Green Greta (Gradio + HF + LangChain v0.2 + Keras 3)
4
- - Keras 3: load SavedModel via keras.layers.TFSMLayer (not load_model)
5
- - LLM: HuggingFaceEndpoint with router-friendly Zephyr by default
6
- - LangChain v0.2 import layout (core/community/text-splitters)
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
- # Pydantic (for typed schema in prompt)
 
 
 
 
 
44
  from pydantic.v1 import BaseModel, Field
45
 
46
- # Hugging Face Hub helpers
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 returns a dict of tensors.
81
  """
82
  img = input_image.convert("RGB").resize((224, 224)) # EfficientNetB0 expects 224x224
83
- image_array = tf.keras.preprocessing.image.img_to_array(img)
84
- image_array = tf.keras.applications.efficientnet.preprocess_input(image_array)
85
- image_array = tf.expand_dims(image_array, 0) # [1, 224, 224, 3]
86
 
87
- # TFSMLayer returns a dict for SavedModel; select the first output
88
- outputs = model1(image_array)
89
  if isinstance(outputs, dict) and outputs:
90
- first_key = next(iter(outputs.keys()))
91
- preds = outputs[first_key]
92
  else:
93
  preds = outputs
94
 
@@ -188,33 +190,26 @@ qa_prompt = ChatPromptTemplate.from_template(
188
 
189
 
190
  # =============================
191
- # 4) LLM (router-friendly HF)
192
  # =============================
193
- DEFAULT_REPO = os.environ.get("HF_REPO_ID", "HuggingFaceH4/zephyr-7b-beta")
194
-
195
- llm = HuggingFaceEndpoint(
196
- repo_id=DEFAULT_REPO,
197
- task="text-generation",
198
- max_new_tokens=1024,
199
- temperature=0.2,
200
- top_k=50,
201
- repetition_penalty=1.05,
202
- do_sample=True,
203
- # Requires env: HUGGINGFACEHUB_API_TOKEN=hf_xxx
 
 
 
 
204
  )
205
 
206
- # If you deploy a paid Inference Endpoint (e.g., for Mixtral), use:
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
  # ===========================================