Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,21 @@
|
|
| 1 |
## Deploying on HuggingFace
|
| 2 |
|
| 3 |
-
|
| 4 |
import streamlit as st
|
| 5 |
import pandas as pd
|
| 6 |
import torch
|
| 7 |
import os
|
| 8 |
from huggingface_hub import login
|
| 9 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 10 |
from peft import PeftModel, PeftConfig
|
| 11 |
import io
|
| 12 |
|
| 13 |
# Login using Hugging Face token stored in Space secrets
|
| 14 |
login(token=os.getenv("HUGGINGFACEHUB_TOKEN"))
|
| 15 |
|
| 16 |
-
|
| 17 |
st.set_page_config(page_title="AnthroBot", page_icon="π€", layout="centered")
|
| 18 |
|
| 19 |
# Load model & tokenizer
|
| 20 |
@st.cache_resource
|
| 21 |
-
|
| 22 |
def load_model():
|
| 23 |
try:
|
| 24 |
peft_config = PeftConfig.from_pretrained("SallySims/AnthroBot_Model_Lora")
|
|
@@ -35,14 +32,13 @@ def load_model():
|
|
| 35 |
|
| 36 |
st.write("β
Model and tokenizer loaded successfully.")
|
| 37 |
return model, tokenizer
|
| 38 |
-
|
| 39 |
except Exception as e:
|
| 40 |
st.error(f"Error loading model: {str(e)}")
|
| 41 |
-
raise e
|
|
|
|
| 42 |
model, tokenizer = load_model()
|
| 43 |
|
| 44 |
# Prediction function
|
| 45 |
-
|
| 46 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 47 |
|
| 48 |
def get_prediction(prompt):
|
|
@@ -52,44 +48,70 @@ def get_prediction(prompt):
|
|
| 52 |
messages = [{"role": "user", "content": prompt}]
|
| 53 |
|
| 54 |
# Tokenize the input
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# Decode the output
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
| 90 |
# UI Header
|
| 91 |
st.title("π§ AnthroBot")
|
| 92 |
-
st.write("Enter your anthropometric estimates to receive an interpreted summary
|
| 93 |
|
| 94 |
# Tabs for input method
|
| 95 |
tab1, tab2 = st.tabs(["π§ Manual Input", "π CSV Upload"])
|
|
@@ -105,8 +127,9 @@ with tab1:
|
|
| 105 |
if st.button("Get Prediction"):
|
| 106 |
prompt = f"Age: {age}, Sex: {sex}, Height: {height} cm, Weight: {weight} kg, WC: {wc} cm\n\n###"
|
| 107 |
prediction = get_prediction(prompt)
|
| 108 |
-
|
| 109 |
-
|
|
|
|
| 110 |
|
| 111 |
with tab2:
|
| 112 |
st.subheader("Batch Upload via CSV")
|
|
@@ -135,13 +158,11 @@ with tab2:
|
|
| 135 |
f"Weight: {row['Weight']} kg, WC: {row['WC']} cm\n\n###"
|
| 136 |
)
|
| 137 |
prediction = get_prediction(prompt)
|
| 138 |
-
outputs.append(prediction)
|
| 139 |
|
| 140 |
df["Prediction"] = outputs
|
| 141 |
st.success("Here are your predictions:")
|
| 142 |
st.dataframe(df)
|
| 143 |
|
| 144 |
csv_output = df.to_csv(index=False).encode("utf-8")
|
| 145 |
-
st.download_button("π€ Download Predictions", data=csv_output, file_name="predictions.csv")
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 1 |
## Deploying on HuggingFace
|
| 2 |
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
import pandas as pd
|
| 5 |
import torch
|
| 6 |
import os
|
| 7 |
from huggingface_hub import login
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
from peft import PeftModel, PeftConfig
|
| 10 |
import io
|
| 11 |
|
| 12 |
# Login using Hugging Face token stored in Space secrets
|
| 13 |
login(token=os.getenv("HUGGINGFACEHUB_TOKEN"))
|
| 14 |
|
|
|
|
| 15 |
st.set_page_config(page_title="AnthroBot", page_icon="π€", layout="centered")
|
| 16 |
|
| 17 |
# Load model & tokenizer
|
| 18 |
@st.cache_resource
|
|
|
|
| 19 |
def load_model():
|
| 20 |
try:
|
| 21 |
peft_config = PeftConfig.from_pretrained("SallySims/AnthroBot_Model_Lora")
|
|
|
|
| 32 |
|
| 33 |
st.write("β
Model and tokenizer loaded successfully.")
|
| 34 |
return model, tokenizer
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
st.error(f"Error loading model: {str(e)}")
|
| 37 |
+
raise e
|
| 38 |
+
|
| 39 |
model, tokenizer = load_model()
|
| 40 |
|
| 41 |
# Prediction function
|
|
|
|
| 42 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 43 |
|
| 44 |
def get_prediction(prompt):
|
|
|
|
| 48 |
messages = [{"role": "user", "content": prompt}]
|
| 49 |
|
| 50 |
# Tokenize the input
|
| 51 |
+
try:
|
| 52 |
+
inputs = tokenizer.apply_chat_template(
|
| 53 |
+
messages,
|
| 54 |
+
tokenize=True,
|
| 55 |
+
add_generation_prompt=True,
|
| 56 |
+
return_tensors="pt",
|
| 57 |
+
max_length=512,
|
| 58 |
+
truncation=True
|
| 59 |
+
).to(device)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
st.error(f"Error during tokenization: {str(e)}")
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
+
# Debug: Log inputs structure
|
| 65 |
+
st.write(f"Inputs type: {type(inputs)}")
|
| 66 |
+
if isinstance(inputs, dict):
|
| 67 |
+
st.write(f"Inputs keys: {list(inputs.keys())}")
|
| 68 |
+
if 'input_ids' in inputs:
|
| 69 |
+
st.write(f"Input IDs shape: {inputs['input_ids'].shape}")
|
| 70 |
+
else:
|
| 71 |
+
st.error("No 'input_ids' in tokenized inputs")
|
| 72 |
+
return None
|
| 73 |
+
else:
|
| 74 |
+
st.error(f"Unexpected inputs format: {type(inputs)}")
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
# Extract input_ids safely
|
| 78 |
+
input_ids = inputs['input_ids']
|
| 79 |
+
if len(input_ids.shape) == 3 and input_ids.shape[0] == 1:
|
| 80 |
+
input_ids = input_ids.squeeze(0) # Remove batch dimension if 3D
|
| 81 |
+
elif len(input_ids.shape) == 2:
|
| 82 |
+
pass # Already 2D, no squeeze needed
|
| 83 |
+
else:
|
| 84 |
+
st.error(f"Invalid input_ids shape: {input_ids.shape}")
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
st.write(f"Final input_ids shape: {input_ids.shape}")
|
| 88 |
+
|
| 89 |
+
# Generate output
|
| 90 |
+
try:
|
| 91 |
+
output = model.generate(
|
| 92 |
+
input_ids=input_ids,
|
| 93 |
+
max_new_tokens=150,
|
| 94 |
+
temperature=0.7,
|
| 95 |
+
top_p=0.95,
|
| 96 |
+
do_sample=True,
|
| 97 |
+
pad_token_id=tokenizer.pad_token_id
|
| 98 |
+
)
|
| 99 |
+
except Exception as e:
|
| 100 |
+
st.error(f"Error during generation: {str(e)}")
|
| 101 |
+
return None
|
| 102 |
|
| 103 |
# Decode the output
|
| 104 |
+
try:
|
| 105 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 106 |
+
st.write(f"Decoded output: {decoded}")
|
| 107 |
+
return decoded
|
| 108 |
+
except Exception as e:
|
| 109 |
+
st.error(f"Error decoding output: {str(e)}")
|
| 110 |
+
return None
|
| 111 |
|
| 112 |
# UI Header
|
| 113 |
st.title("π§ AnthroBot")
|
| 114 |
+
st.write("Enter your anthropometric estimates to receive an interpreted summary β manually or via CSV upload.")
|
| 115 |
|
| 116 |
# Tabs for input method
|
| 117 |
tab1, tab2 = st.tabs(["π§ Manual Input", "π CSV Upload"])
|
|
|
|
| 127 |
if st.button("Get Prediction"):
|
| 128 |
prompt = f"Age: {age}, Sex: {sex}, Height: {height} cm, Weight: {weight} kg, WC: {wc} cm\n\n###"
|
| 129 |
prediction = get_prediction(prompt)
|
| 130 |
+
if prediction:
|
| 131 |
+
st.success("Prediction:")
|
| 132 |
+
st.write(prediction)
|
| 133 |
|
| 134 |
with tab2:
|
| 135 |
st.subheader("Batch Upload via CSV")
|
|
|
|
| 158 |
f"Weight: {row['Weight']} kg, WC: {row['WC']} cm\n\n###"
|
| 159 |
)
|
| 160 |
prediction = get_prediction(prompt)
|
| 161 |
+
outputs.append(prediction if prediction else "Error")
|
| 162 |
|
| 163 |
df["Prediction"] = outputs
|
| 164 |
st.success("Here are your predictions:")
|
| 165 |
st.dataframe(df)
|
| 166 |
|
| 167 |
csv_output = df.to_csv(index=False).encode("utf-8")
|
| 168 |
+
st.download_button("π€ Download Predictions", data=csv_output, file_name="predictions.csv")
|
|
|
|
|
|