rockerritesh commited on
Commit
bf3f219
·
verified ·
1 Parent(s): 3406026

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from typing import List
3
+ import requests
4
+ import json
5
+
6
+ def get_text_embedding(text: str, model: str = "mxbai-embed-large", api_url: str = "http://localhost:11434/api/embeddings") -> List[float]:
7
+ """
8
+ Sends a prompt to the embedding API and retrieves the embedding.
9
+
10
+ Args:
11
+ text (str): The text to embed.
12
+ model (str): The model to use for generating the embedding (default is "mxbai-embed-large").
13
+ api_url (str): The API endpoint URL (default is "http://localhost:11434/api/embeddings").
14
+
15
+ Returns:
16
+ list: A list of floats representing the embedding vector.
17
+
18
+ Raises:
19
+ Exception: If the API request fails.
20
+ """
21
+ payload = {
22
+ "model": model,
23
+ "prompt": text
24
+ }
25
+
26
+ try:
27
+ response = requests.post(api_url, data=json.dumps(payload), headers={"Content-Type": "application/json"})
28
+ response.raise_for_status() # Raise an error for non-200 status codes
29
+ data = response.json()
30
+ return data.get("embedding", [])
31
+ except requests.exceptions.RequestException as e:
32
+ raise Exception(f"Error communicating with the embedding API: {e}")
33
+
34
+ def process_text_to_embedding(text: str) -> str:
35
+ """Process the text input and return the embedding as a string."""
36
+ try:
37
+ embedding = get_text_embedding(text)
38
+ return json.dumps(embedding, indent=2)
39
+ except Exception as e:
40
+ return f"Error: {str(e)}"
41
+
42
+ # Define the Gradio interface
43
+ def main():
44
+ title = "Text Embedding Generator"
45
+ description = "Enter a text input, and this tool will generate an embedding using the specified model via API."
46
+
47
+ with gr.Blocks() as demo:
48
+ gr.Markdown(f"# {title}")
49
+ gr.Markdown(description)
50
+
51
+ with gr.Row():
52
+ text_input = gr.Textbox(label="Input Text", placeholder="Enter your text here")
53
+
54
+ with gr.Row():
55
+ output = gr.Textbox(label="Embedding Output", lines=10)
56
+
57
+ submit_button = gr.Button("Generate Embedding")
58
+
59
+ submit_button.click(fn=process_text_to_embedding, inputs=[text_input], outputs=[output])
60
+
61
+ demo.launch()
62
+
63
+ if __name__ == "__main__":
64
+ main()