TiniThingsInc commited on
Commit
2ec6494
·
verified ·
1 Parent(s): 7bdfa11

creating app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import SentenceTransformer
3
+ import numpy as np
4
+ from typing import List, Union
5
+ import spaces # ZeroGPU decorator
6
+
7
+ # Load model once at startup
8
+ MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
9
+ model = SentenceTransformer(MODEL_NAME)
10
+
11
+ @spaces.GPU(duration=60) # ZeroGPU: allocate GPU for 60 seconds
12
+ def generate_embeddings(texts: Union[str, List[str]]) -> List[List[float]]:
13
+ """
14
+ Generate embeddings for text(s)
15
+
16
+ Args:
17
+ texts: Single string or list of strings
18
+
19
+ Returns:
20
+ List of embedding vectors (384 dimensions)
21
+ """
22
+ # Handle single string
23
+ if isinstance(texts, str):
24
+ texts = [texts]
25
+
26
+ # Generate embeddings
27
+ embeddings = model.encode(
28
+ texts,
29
+ convert_to_numpy=True,
30
+ normalize_embeddings=True, # Normalize for cosine similarity
31
+ show_progress_bar=False
32
+ )
33
+
34
+ # Convert to list for JSON serialization
35
+ return embeddings.tolist()
36
+
37
+ def batch_generate(texts_input: str) -> str:
38
+ """
39
+ Gradio interface for batch embedding generation
40
+ Expects newline-separated texts
41
+ """
42
+ if not texts_input.strip():
43
+ return "Error: Please provide at least one text"
44
+
45
+ texts = [t.strip() for t in texts_input.split('\n') if t.strip()]
46
+
47
+ try:
48
+ embeddings = generate_embeddings(texts)
49
+
50
+ result = f"Generated {len(embeddings)} embeddings\n"
51
+ result += f"Dimensions: {len(embeddings[0])}\n\n"
52
+ result += "First embedding preview:\n"
53
+ result += str(embeddings[0][:10]) + "...\n"
54
+
55
+ return result
56
+ except Exception as e:
57
+ return f"Error: {str(e)}"
58
+
59
+ # Create Gradio interface
60
+ with gr.Blocks(title="FairFate Embeddings API") as demo:
61
+ gr.Markdown("""
62
+ # FairFate Embeddings API
63
+
64
+ Generate semantic embeddings using sentence-transformers/all-MiniLM-L6-v2
65
+ - **Dimensions:** 384
66
+ - **Max Tokens:** 256
67
+ - **Normalized:** Yes (ready for cosine similarity)
68
+
69
+ ## API Usage
70
+
71
+ **Endpoint:** `https://your-username-fairfate-embeddings.hf.space/api/predict`
72
+
73
+ **Request:**
74
+ ```json
75
+ {
76
+ "data": ["Your text here", "Another text"]
77
+ }
78
+ ```
79
+
80
+ **Response:**
81
+ ```json
82
+ {
83
+ "data": [[[0.123, -0.456, ...]], [[0.789, -0.012, ...]]]
84
+ }
85
+ ```
86
+ """)
87
+
88
+ with gr.Tab("Test Embeddings"):
89
+ input_text = gr.Textbox(
90
+ label="Input Texts (one per line)",
91
+ placeholder="Enter texts, one per line:\nDungeons and Dragons adventure\nSci-fi space opera campaign",
92
+ lines=5
93
+ )
94
+ output_text = gr.Textbox(label="Results", lines=10)
95
+ submit_btn = gr.Button("Generate Embeddings")
96
+ submit_btn.click(batch_generate, inputs=input_text, outputs=output_text)
97
+
98
+ with gr.Tab("API Documentation"):
99
+ gr.Markdown("""
100
+ ### Python Example
101
+ ```python
102
+ import requests
103
+
104
+ response = requests.post(
105
+ "https://your-username-fairfate-embeddings.hf.space/api/predict",
106
+ json={"data": ["Your text here"]}
107
+ )
108
+ embeddings = response.json()["data"][0]
109
+ ```
110
+
111
+ ### JavaScript/TypeScript Example
112
+ ```typescript
113
+ const response = await fetch(
114
+ 'https://your-username-fairfate-embeddings.hf.space/api/predict',
115
+ {
116
+ method: 'POST',
117
+ headers: { 'Content-Type': 'application/json' },
118
+ body: JSON.stringify({ data: ["Your text here"] })
119
+ }
120
+ );
121
+ const result = await response.json();
122
+ const embeddings = result.data[0];
123
+ ```
124
+ """)
125
+ # Launch with API enabled
126
+ demo.launch()