borgo9 commited on
Commit
4ff412d
·
verified ·
1 Parent(s): b82ee61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -15
app.py CHANGED
@@ -77,24 +77,67 @@
77
  # demo.launch()
78
 
79
 
80
- import gradio as gr
81
  import os
82
-
83
- HF_TOKEN = os.getenv("HF_TOKEN") # Will be None if no secret is set
84
-
85
- # Simple function that echoes the input
86
- def echo(text):
87
- return f"You typed: {text}"
88
-
89
- # Build a minimal Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  demo = gr.Interface(
91
- fn=echo,
92
- inputs=gr.Textbox(label="Type something"),
93
- outputs=gr.Textbox(label="Output"),
94
- title="Test Gradio Space",
95
- description="This is a minimal app to test if the Space starts."
96
  )
97
 
98
- # Launch the app
 
 
99
  if __name__ == "__main__":
100
  demo.launch()
 
 
77
  # demo.launch()
78
 
79
 
 
80
  import os
81
+ import gradio as gr
82
+ from transformers import AutoTokenizer, AutoModel
83
+ import torch
84
+ import numpy as np
85
+
86
+ # ----------------------------------------------------
87
+ # 1. HF_TOKEN (optional, for private models)
88
+ # ----------------------------------------------------
89
+ HF_TOKEN = os.getenv("HF_TOKEN") # Retrieved from Hugging Face Secrets
90
+
91
+ # ----------------------------------------------------
92
+ # 2. Load EmbeddingGemma-300M model
93
+ # ----------------------------------------------------
94
+ MODEL_NAME = "google/embeddinggemma-300m"
95
+
96
+ # Load tokenizer and model
97
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=HF_TOKEN)
98
+ model = AutoModel.from_pretrained(MODEL_NAME, use_auth_token=HF_TOKEN)
99
+
100
+ # Function to encode text into embeddings
101
+ def encode(texts):
102
+ """
103
+ Encode a list of texts into vector embeddings using EmbeddingGemma-300M.
104
+ Mean pooling over token embeddings is used.
105
+ """
106
+ inputs = tokenizer(
107
+ texts, return_tensors="pt", padding=True, truncation=True
108
+ )
109
+ with torch.no_grad():
110
+ outputs = model(**inputs)
111
+ # Mean pooling over tokens
112
+ embeddings = outputs.last_hidden_state.mean(dim=1)
113
+ # Convert to numpy float32
114
+ return embeddings.cpu().numpy().astype(np.float32)
115
+
116
+ # ----------------------------------------------------
117
+ # 3. Gradio test function
118
+ # ----------------------------------------------------
119
+ def test_encode(text):
120
+ """
121
+ Simple test function to check if embeddings are generated correctly.
122
+ Returns the shape of the resulting embedding vector.
123
+ """
124
+ emb = encode([text])
125
+ return f"Embedding shape: {emb.shape}"
126
+
127
+ # ----------------------------------------------------
128
+ # 4. Build Gradio Interface
129
+ # ----------------------------------------------------
130
  demo = gr.Interface(
131
+ fn=test_encode,
132
+ inputs=gr.Textbox(label="Type some text"),
133
+ outputs=gr.Textbox(label="Embedding info"),
134
+ title="Test EmbeddingGemma-300M",
135
+ description="This Space tests whether the EmbeddingGemma-300M model can generate embeddings."
136
  )
137
 
138
+ # ----------------------------------------------------
139
+ # 5. Launch App
140
+ # ----------------------------------------------------
141
  if __name__ == "__main__":
142
  demo.launch()
143
+