Leonydis137 commited on
Commit
1388209
·
verified ·
1 Parent(s): f973220

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -106
app.py CHANGED
@@ -1,115 +1,33 @@
1
- import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return f"Hello {name}! Autonomous AI initialized."
 
 
 
5
 
6
- with gr.Blocks() as demo:
7
- name = gr.Textbox(label="Your Name")
8
- output = gr.Textbox(label="Greeting")
9
- name.change(greet, name, output)
10
 
11
- demo.launch()
 
12
 
13
- from fastapi import FastAPI
14
- from fastapi.middleware.cors import CORSMiddleware
15
- from fastapi.responses import HTMLResponse
16
- from fastapi.staticfiles import StaticFiles
17
- import gradio as gr
18
- import numpy as np
19
- import faiss
20
- import logging
21
- import os
22
- import requests
23
- import json
24
- from sentence_transformers import SentenceTransformer
25
 
26
- # === Environment variables (safe cache paths for HF Spaces) ===
27
- os.environ["TRANSFORMERS_CACHE"] = "/data/transformers"
28
- os.environ["HF_HOME"] = "/data/hf"
29
- os.environ["SENTENCE_TRANSFORMERS_HOME"] = "/data/st"
30
 
31
- # === Load embedding model ===
32
- model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
33
- index = faiss.IndexFlatL2(384)
34
- memory_text = []
35
 
36
- # === Fireworks API ===
37
- FIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY") # 🔐 Use env var instead of hardcoding
38
- FIREWORKS_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
39
 
40
- def query_fireworks(prompt):
41
- payload = {
42
- "model": "accounts/fireworks/models/deepseek-r1",
43
- "max_tokens": 4096,
44
- "top_p": 1,
45
- "top_k": 40,
46
- "temperature": 0.6,
47
- "messages": [{"role": "user", "content": prompt}],
48
- }
49
- headers = {
50
- "Accept": "application/json",
51
- "Content-Type": "application/json",
52
- "Authorization": f"Bearer {FIREWORKS_API_KEY}"
53
- }
54
 
55
- response = requests.post(FIREWORKS_URL, headers=headers, data=json.dumps(payload))
56
- result = response.json()
57
- return result.get("choices", [{}])[0].get("message", {}).get("content", "⚠️ No response.")
58
-
59
- # === Autonomous Agent ===
60
- def autonomous_agent(input_text):
61
- vec = model.encode([input_text])[0]
62
- response = ""
63
-
64
- if index.ntotal > 0:
65
- D, I = index.search(np.array([vec]), min(5, index.ntotal))
66
- matches = [memory_text[idx] for idx, dist in zip(I[0], D[0]) if idx != -1 and dist < 0.8]
67
- if matches:
68
- response += "🧠 Related memories:\n- " + "\n- ".join(matches[:3]) + "\n\n"
69
-
70
- # Store current memory
71
- index.add(np.array([vec]))
72
- memory_text.append(input_text)
73
-
74
- # 🔥 Query LLM (Fireworks)
75
- llm_response = query_fireworks(input_text)
76
- response += f"🤖 Response:\n{llm_response}"
77
- return response
78
-
79
- # === Gradio UI ===
80
- gradio_ui = gr.Interface(
81
- fn=autonomous_agent,
82
- inputs="text",
83
- outputs="text",
84
- title="Autonomous AI Agent",
85
- description="Self-enhancing chatbot with memory + Fireworks LLM",
86
- )
87
-
88
- # === FastAPI App ===
89
- app = FastAPI()
90
-
91
- app.add_middleware(
92
- CORSMiddleware,
93
- allow_origins=["*"], allow_methods=["*"], allow_headers=["*"],
94
- )
95
-
96
- # === Root Web UI with embedded Gradio ===
97
- @app.get("/", response_class=HTMLResponse)
98
- async def root():
99
- return """
100
- <html>
101
- <head><title>Autonomous AI Agent</title></head>
102
- <body>
103
- <h2>Autonomous AI Agent</h2>
104
- <iframe src="/gradio" width="100%" height="600"></iframe>
105
- </body>
106
- </html>
107
- """
108
-
109
- # === Mount Gradio & static files ===
110
- app.mount("/gradio", gradio_ui.app)
111
- app.mount("/static", StaticFiles(directory="static"), name="static")
112
-
113
- # === For Hugging Face Spaces ===
114
- def get_app():
115
- return app
 
1
+ from transformers import AutoTokenizer, AutoModel
2
+ import torch
3
+ import torch.nn.functional as F
4
 
5
+ #Mean Pooling - Take attention mask into account for correct averaging
6
+ def mean_pooling(model_output, attention_mask):
7
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
8
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
9
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
10
 
 
 
 
 
11
 
12
+ # Sentences we want sentence embeddings for
13
+ sentences = ['This is an example sentence', 'Each sentence is converted']
14
 
15
+ # Load model from HuggingFace Hub
16
+ tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
17
+ model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
 
 
 
 
 
 
 
 
 
18
 
19
+ # Tokenize sentences
20
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
 
 
21
 
22
+ # Compute token embeddings
23
+ with torch.no_grad():
24
+ model_output = model(**encoded_input)
 
25
 
26
+ # Perform pooling
27
+ sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
 
28
 
29
+ # Normalize embeddings
30
+ sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ print("Sentence embeddings:")
33
+ print(sentence_embeddings)