dakshdarji01 commited on
Commit
ab1bbca
·
verified ·
1 Parent(s): 148ddb3

Upload 9 files

Browse files
Files changed (9) hide show
  1. .dockerignore +5 -0
  2. .env +2 -0
  3. .firebaserc +5 -0
  4. Dockerfile +22 -0
  5. README.md +8 -7
  6. core_logic.py +225 -0
  7. firebase.json +19 -0
  8. requirements.txt +7 -0
  9. server.py +266 -0
.dockerignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ __pycache__/
2
+ *.pyc
3
+ .env
4
+ venv/
5
+ .venv/
.env ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # Environment variables for the AI Browser backend
2
+ OPENROUTER_API_KEY=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
.firebaserc ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "projects": {
3
+ "default": "verse-ai-browser"
4
+ }
5
+ }
Dockerfile ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install system dependencies required for ML libraries
6
+ RUN apt-get update && apt-get install -y \
7
+ build-essential \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ # Install Python dependencies
11
+ COPY requirements.txt .
12
+ RUN pip install --no-cache-dir -r requirements.txt
13
+
14
+ # Copy backend code
15
+ COPY . .
16
+
17
+ # Expose port (Hugging Face Spaces defaults to 7860)
18
+ ENV PORT=7860
19
+ EXPOSE 7860
20
+
21
+ # Run the FastAPI server
22
+ CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,11 +1,12 @@
1
  ---
2
- title: Verse Ai Browser
3
- emoji: ⚡
4
- colorFrom: gray
5
- colorTo: blue
6
  sdk: docker
7
- pinned: false
8
- license: mit
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
  ---
2
+ title: AI Browser Backend
3
+ emoji: 🚀
4
+ colorFrom: blue
5
+ colorTo: purple
6
  sdk: docker
7
+ app_port: 7860
 
8
  ---
9
 
10
+ # AI Browser Backend
11
+
12
+ This is the FastAPI backend for the AI Browser, powered by `sentence-transformers` and `torch`.
core_logic.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ # pyrefly: ignore [missing-import]
3
+ import numpy as np
4
+ # pyrefly: ignore [missing-import]
5
+ from bs4 import BeautifulSoup
6
+ # pyrefly: ignore [missing-import]
7
+ from openai import OpenAI
8
+
9
+ _model = None
10
+
11
+ def get_model():
12
+ """Lazily load the SentenceTransformer model to save memory/startup time."""
13
+ global _model
14
+ if _model is None:
15
+ # pyrefly: ignore [missing-import]
16
+ from sentence_transformers import SentenceTransformer
17
+ _model = SentenceTransformer('all-MiniLM-L6-v2')
18
+ return _model
19
+
20
+ def extract_clean_prose(html_content: str) -> str:
21
+ """Extract clean prose text from HTML, stripping script, style, and nav tags."""
22
+ if not html_content:
23
+ return ""
24
+ soup = BeautifulSoup(html_content, 'html.parser')
25
+ for tag in soup(["script", "style", "nav"]):
26
+ tag.decompose()
27
+ raw_text = soup.get_text(separator=' ')
28
+ clean_text = ' '.join(raw_text.split())
29
+ return clean_text
30
+
31
+ def chunk_text(text: str, chunk_size: int = 50, overlap: int = 10) -> list[str]:
32
+ """Chunk text using a sliding window based on word count."""
33
+ words = text.split()
34
+ if not words:
35
+ return []
36
+ chunks = []
37
+ step = chunk_size - overlap
38
+ if step <= 0:
39
+ step = 1 # Avoid infinite loop
40
+
41
+ for i in range(0, len(words), step):
42
+ chunk_words = words[i:i + chunk_size]
43
+ chunk_text = ' '.join(chunk_words)
44
+ chunks.append(chunk_text)
45
+ if i + chunk_size >= len(words):
46
+ break
47
+
48
+ return chunks
49
+
50
+ def embed_texts(texts: list[str]) -> list[list[float]]:
51
+ """Generate embeddings for a list of texts using SentenceTransformer."""
52
+ if not texts:
53
+ return []
54
+ model = get_model()
55
+ embeddings = model.encode(texts, show_progress_bar=False)
56
+ # Convert numpy array to standard python list of lists
57
+ return embeddings.tolist()
58
+
59
+ def cosine_similarity(v1, v2) -> float:
60
+ """Compute cosine similarity between two vectors."""
61
+ dot_product = np.dot(v1, v2)
62
+ norm_v1 = np.linalg.norm(v1)
63
+ norm_v2 = np.linalg.norm(v2)
64
+ if norm_v1 == 0 or norm_v2 == 0:
65
+ return 0.0
66
+ return float(dot_product / (norm_v1 * norm_v2))
67
+
68
+ def retrieve_top_chunks(query: str, chunks: list[str], chunk_embeddings: list[list[float]], top_k: int = 2) -> list[dict]:
69
+ """Retrieve top_k matching chunks for a query using cosine similarity."""
70
+ if not chunks or not chunk_embeddings:
71
+ return []
72
+
73
+ # Generate query embedding
74
+ model = get_model()
75
+ query_emb = model.encode(query, show_progress_bar=False)
76
+
77
+ scores = []
78
+ for idx, emb_list in enumerate(chunk_embeddings):
79
+ emb = np.array(emb_list)
80
+ score = cosine_similarity(query_emb, emb)
81
+ scores.append({
82
+ "score": score,
83
+ "index": idx,
84
+ "text": chunks[idx]
85
+ })
86
+
87
+ # Sort by score descending
88
+ scores.sort(key=lambda x: x["score"], reverse=True)
89
+ return scores[:top_k]
90
+
91
+
92
+ # --- LLM Helper ---
93
+
94
+ def _call_llm(system_prompt: str, user_text: str, api_key: str | None = None) -> str:
95
+ """Internal helper to call OpenRouter LLM with a given system prompt and user text."""
96
+ if not api_key:
97
+ api_key = os.environ.get("OPENROUTER_API_KEY")
98
+ if not api_key:
99
+ raise ValueError(
100
+ "No API key provided. Set the OPENROUTER_API_KEY environment variable "
101
+ "or pass api_key in the request."
102
+ )
103
+
104
+ client = OpenAI(
105
+ base_url="https://openrouter.ai/api/v1",
106
+ api_key=api_key,
107
+ )
108
+
109
+ models_to_try = [
110
+ "openai/gpt-oss-120b:free",
111
+ "google/gemma-4-31b-it:free",
112
+ "nvidia/nemotron-3-super-120b-a12b:free",
113
+ "qwen/qwen3-next-80b-a3b-instruct:free"
114
+ ]
115
+
116
+ last_error = None
117
+ for model in models_to_try:
118
+ try:
119
+ response = client.chat.completions.create(
120
+ model=model,
121
+ messages=[
122
+ {"role": "system", "content": system_prompt},
123
+ {"role": "user", "content": user_text}
124
+ ]
125
+ )
126
+ return response.choices[0].message.content
127
+ except Exception as e:
128
+ last_error = e
129
+ continue
130
+
131
+ # If all models fail, return a polite fallback message instead of crashing with a 500 error
132
+ error_str = str(last_error)
133
+ if "429" in error_str or "rate-limited" in error_str.lower():
134
+ return "I am currently overloaded with requests (Rate Limited by the provider). Please try clicking the action button again in a few seconds!"
135
+ else:
136
+ return "I encountered an error connecting to the AI provider. Please try again later."
137
+
138
+
139
+ def query_llm(query: str, context: str, api_key: str | None = None, title: str = "", url: str = "", selected_text: str = "") -> str:
140
+ """Use OpenRouter LLM to answer a query based on the provided context and metadata."""
141
+ system_prompt = (
142
+ "You are an AI assistant built into the 'Verse' browser. Answer accurately using the provided webpage context. "
143
+ "Be extremely concise and direct. Output NO introductory or conversational filler text."
144
+ )
145
+
146
+ metadata_lines = []
147
+ if url:
148
+ metadata_lines.append(f"Page URL: {url}")
149
+ if title:
150
+ metadata_lines.append(f"Page Title: {title}")
151
+ if selected_text:
152
+ metadata_lines.append(f"User's Highlighted Text: {selected_text}")
153
+
154
+ metadata_block = "\n".join(metadata_lines)
155
+ if metadata_block:
156
+ metadata_block = f"--- Page Metadata ---\n{metadata_block}\n---------------------\n\n"
157
+
158
+ user_text = f"{metadata_block}Context Snippets:\n{context}\n\nUser Query: {query}"
159
+ return _call_llm(system_prompt, user_text, api_key)
160
+
161
+
162
+ # --- AI Action Functions ---
163
+
164
+ def summarize_text(text: str, api_key: str | None = None) -> str:
165
+ """Summarize the given text concisely."""
166
+ system_prompt = (
167
+ "Summarize concisely using short bullet points. Output NO filler or conversational text."
168
+ )
169
+ return _call_llm(system_prompt, text, api_key)
170
+
171
+ def explain_text(text: str, api_key: str | None = None) -> str:
172
+ """Explain the given text in simple terms."""
173
+ system_prompt = (
174
+ "Explain simply for a beginner. Be extremely concise. Output NO filler text."
175
+ )
176
+ return _call_llm(system_prompt, text, api_key)
177
+
178
+ def translate_text(text: str, target_language: str, api_key: str | None = None) -> str:
179
+ """Translate text to the target language."""
180
+ system_prompt = (
181
+ f"Translate the text to {target_language}. Output ONLY the raw translation with NO introductory text."
182
+ )
183
+ return _call_llm(system_prompt, text, api_key)
184
+
185
+ def extract_data(text: str, data_type: str, api_key: str | None = None) -> str:
186
+ """Extract specific data (emails, phones, links) from text."""
187
+ system_prompt = (
188
+ f"Extract all {data_type}. Output ONLY a clean list, one item per line. No filler."
189
+ )
190
+ return _call_llm(system_prompt, text, api_key)
191
+
192
+ def generate_post(text: str, platform: str, api_key: str | None = None) -> str:
193
+ """Generate a social media post from the text."""
194
+ system_prompt = (
195
+ f"Create a short, engaging {platform} post based on the text. Include hashtags. Output ONLY the post content."
196
+ )
197
+ return _call_llm(system_prompt, text, api_key)
198
+
199
+ def rewrite_text(text: str, style: str, api_key: str | None = None) -> str:
200
+ """Rewrite text in a specified style."""
201
+ system_prompt = (
202
+ f"Rewrite in a {style} style. Output ONLY the rewritten text with NO filler."
203
+ )
204
+ return _call_llm(system_prompt, text, api_key)
205
+
206
+ def list_key_points(text: str, api_key: str | None = None) -> str:
207
+ """Extract key points from the text."""
208
+ system_prompt = (
209
+ "Extract key points as a numbered list. Be extremely brief. Output NO filler text."
210
+ )
211
+ return _call_llm(system_prompt, text, api_key)
212
+
213
+ def generate_faqs(text: str, api_key: str | None = None) -> str:
214
+ """Generate FAQs from the text."""
215
+ system_prompt = (
216
+ "Generate 3-5 FAQs based on the text. Format as 'Q: ...' 'A: ...'. Keep answers strictly concise. Output NO filler."
217
+ )
218
+ return _call_llm(system_prompt, text, api_key)
219
+
220
+ def create_study_notes(text: str, api_key: str | None = None) -> str:
221
+ """Create study notes from the text."""
222
+ system_prompt = (
223
+ "Create brief study notes with headings and bullet points. Be extremely concise. Output NO filler text."
224
+ )
225
+ return _call_llm(system_prompt, text, api_key)
firebase.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "hosting": {
3
+ "public": "public",
4
+ "ignore": [
5
+ "firebase.json",
6
+ "**/.*",
7
+ "**/node_modules/**"
8
+ ],
9
+ "rewrites": [
10
+ {
11
+ "source": "**",
12
+ "run": {
13
+ "serviceId": "ai-browser-backend",
14
+ "region": "us-central1"
15
+ }
16
+ }
17
+ ]
18
+ }
19
+ }
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi>=0.100.0
2
+ uvicorn>=0.22.0
3
+ python-dotenv>=1.0.0
4
+ beautifulsoup4>=4.12.0
5
+ sentence-transformers>=2.2.0
6
+ numpy>=1.20.0
7
+ openai>=1.0.0
server.py ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # pyrefly: ignore [missing-import]
2
+ from dotenv import load_dotenv
3
+ load_dotenv()
4
+
5
+ import os
6
+ # pyrefly: ignore [missing-import]
7
+ from fastapi import FastAPI, HTTPException
8
+ # pyrefly: ignore [missing-import]
9
+ from fastapi.middleware.cors import CORSMiddleware
10
+ # pyrefly: ignore [missing-import]
11
+ from pydantic import BaseModel, Field
12
+
13
+ # Import core RAG logic functions
14
+ import core_logic
15
+
16
+ app = FastAPI(
17
+ title="AI Browser RAG Backend",
18
+ description="Backend API for text parsing, embedding, retrieval, LLM querying, and AI actions",
19
+ version="2.0.0"
20
+ )
21
+
22
+ # Allow CORS for all origins since it will be deployed
23
+ app.add_middleware(
24
+ CORSMiddleware,
25
+ allow_origins=["*"],
26
+ allow_credentials=True,
27
+ allow_methods=["*"],
28
+ allow_headers=["*"],
29
+ )
30
+
31
+ # --- Pydantic Schemas ---
32
+
33
+ class ProcessHtmlRequest(BaseModel):
34
+ html: str
35
+ chunk_size: int = Field(default=50, ge=1)
36
+ overlap: int = Field(default=10, ge=0)
37
+
38
+ class ProcessHtmlResponse(BaseModel):
39
+ chunks: list[str]
40
+
41
+ class EmbedRequest(BaseModel):
42
+ texts: list[str]
43
+
44
+ class EmbedResponse(BaseModel):
45
+ embeddings: list[list[float]]
46
+
47
+ class RetrieveRequest(BaseModel):
48
+ query: str
49
+ chunks: list[str]
50
+ embeddings: list[list[float]]
51
+ top_k: int = Field(default=2, ge=1)
52
+
53
+ class RetrievalResultItem(BaseModel):
54
+ score: float
55
+ index: int
56
+ text: str
57
+
58
+ class RetrieveResponse(BaseModel):
59
+ results: list[RetrievalResultItem]
60
+
61
+ class AnswerRequest(BaseModel):
62
+ query: str
63
+ context: str
64
+ api_key: str | None = None
65
+
66
+ class AnswerResponse(BaseModel):
67
+ answer: str
68
+
69
+ class UnifiedRagRequest(BaseModel):
70
+ html: str
71
+ query: str
72
+ chunk_size: int = Field(default=50, ge=1)
73
+ overlap: int = Field(default=10, ge=0)
74
+ top_k: int = Field(default=2, ge=1)
75
+ title: str = ""
76
+ url: str = ""
77
+ selected_text: str = ""
78
+ api_key: str | None = None
79
+
80
+ class UnifiedRagResponse(BaseModel):
81
+ answer: str
82
+ retrieved_chunks: list[RetrievalResultItem]
83
+
84
+ # --- AI Action Schemas ---
85
+
86
+ class TextRequest(BaseModel):
87
+ text: str
88
+ api_key: str | None = None
89
+
90
+ class TranslateRequest(BaseModel):
91
+ text: str
92
+ target_language: str
93
+ api_key: str | None = None
94
+
95
+ class ExtractRequest(BaseModel):
96
+ text: str
97
+ data_type: str
98
+ api_key: str | None = None
99
+
100
+ class GeneratePostRequest(BaseModel):
101
+ text: str
102
+ platform: str
103
+ api_key: str | None = None
104
+
105
+ class RewriteRequest(BaseModel):
106
+ text: str
107
+ style: str
108
+ api_key: str | None = None
109
+
110
+ class ActionResponse(BaseModel):
111
+ result: str
112
+
113
+ # --- Route Handlers ---
114
+
115
+ @app.get("/")
116
+ def read_root():
117
+ return {"status": "ok", "message": "AI Browser RAG Backend API is running.", "version": "2.0.0"}
118
+
119
+ @app.post("/process-html", response_model=ProcessHtmlResponse)
120
+ def process_html(payload: ProcessHtmlRequest):
121
+ try:
122
+ clean_text = core_logic.extract_clean_prose(payload.html)
123
+ chunks = core_logic.chunk_text(clean_text, payload.chunk_size, payload.overlap)
124
+ return ProcessHtmlResponse(chunks=chunks)
125
+ except Exception as e:
126
+ raise HTTPException(status_code=500, detail=str(e))
127
+
128
+ @app.post("/embed", response_model=EmbedResponse)
129
+ def embed_chunks(payload: EmbedRequest):
130
+ try:
131
+ embeddings = core_logic.embed_texts(payload.texts)
132
+ return EmbedResponse(embeddings=embeddings)
133
+ except Exception as e:
134
+ raise HTTPException(status_code=500, detail=str(e))
135
+
136
+ @app.post("/retrieve", response_model=RetrieveResponse)
137
+ def retrieve(payload: RetrieveRequest):
138
+ try:
139
+ if len(payload.chunks) != len(payload.embeddings):
140
+ raise HTTPException(status_code=400, detail="The number of chunks must match the number of embeddings.")
141
+ results = core_logic.retrieve_top_chunks(payload.query, payload.chunks, payload.embeddings, payload.top_k)
142
+ return RetrieveResponse(results=results)
143
+ except HTTPException as he:
144
+ raise he
145
+ except Exception as e:
146
+ raise HTTPException(status_code=500, detail=str(e))
147
+
148
+ @app.post("/answer", response_model=AnswerResponse)
149
+ def get_answer(payload: AnswerRequest):
150
+ try:
151
+ answer = core_logic.query_llm(payload.query, payload.context, payload.api_key)
152
+ return AnswerResponse(answer=answer)
153
+ except Exception as e:
154
+ raise HTTPException(status_code=500, detail=str(e))
155
+
156
+ @app.post("/rag", response_model=UnifiedRagResponse)
157
+ def run_unified_rag(payload: UnifiedRagRequest):
158
+ try:
159
+ # Step 1: Clean and chunk HTML
160
+ clean_text = core_logic.extract_clean_prose(payload.html)
161
+ chunks = core_logic.chunk_text(clean_text, payload.chunk_size, payload.overlap)
162
+
163
+ if not chunks:
164
+ return UnifiedRagResponse(answer="No text chunks could be extracted from the HTML content.", retrieved_chunks=[])
165
+
166
+ # Step 2: Embed chunks
167
+ embeddings = core_logic.embed_texts(chunks)
168
+
169
+ # Step 3: Retrieve top chunks
170
+ top_chunks = core_logic.retrieve_top_chunks(payload.query, chunks, embeddings, payload.top_k)
171
+
172
+ # Step 4: Format context and query LLM
173
+ context = "\n\n".join([f"[Source Chunk {item['index'] + 1}]: {item['text']}" for item in top_chunks])
174
+ answer = core_logic.query_llm(
175
+ query=payload.query,
176
+ context=context,
177
+ title=payload.title,
178
+ url=payload.url,
179
+ selected_text=payload.selected_text,
180
+ api_key=payload.api_key
181
+ )
182
+
183
+ return UnifiedRagResponse(answer=answer, retrieved_chunks=top_chunks)
184
+ except Exception as e:
185
+ raise HTTPException(status_code=500, detail=str(e))
186
+
187
+ # --- AI Action Endpoints ---
188
+
189
+ @app.post("/summarize", response_model=ActionResponse)
190
+ def summarize(payload: TextRequest):
191
+ try:
192
+ result = core_logic.summarize_text(payload.text, payload.api_key)
193
+ return ActionResponse(result=result)
194
+ except Exception as e:
195
+ raise HTTPException(status_code=500, detail=str(e))
196
+
197
+ @app.post("/explain", response_model=ActionResponse)
198
+ def explain(payload: TextRequest):
199
+ try:
200
+ result = core_logic.explain_text(payload.text, payload.api_key)
201
+ return ActionResponse(result=result)
202
+ except Exception as e:
203
+ raise HTTPException(status_code=500, detail=str(e))
204
+
205
+ @app.post("/translate", response_model=ActionResponse)
206
+ def translate(payload: TranslateRequest):
207
+ try:
208
+ result = core_logic.translate_text(payload.text, payload.target_language, payload.api_key)
209
+ return ActionResponse(result=result)
210
+ except Exception as e:
211
+ raise HTTPException(status_code=500, detail=str(e))
212
+
213
+ @app.post("/extract", response_model=ActionResponse)
214
+ def extract(payload: ExtractRequest):
215
+ try:
216
+ result = core_logic.extract_data(payload.text, payload.data_type, payload.api_key)
217
+ return ActionResponse(result=result)
218
+ except Exception as e:
219
+ raise HTTPException(status_code=500, detail=str(e))
220
+
221
+ @app.post("/generate-post", response_model=ActionResponse)
222
+ def generate_post(payload: GeneratePostRequest):
223
+ try:
224
+ result = core_logic.generate_post(payload.text, payload.platform, payload.api_key)
225
+ return ActionResponse(result=result)
226
+ except Exception as e:
227
+ raise HTTPException(status_code=500, detail=str(e))
228
+
229
+ @app.post("/rewrite", response_model=ActionResponse)
230
+ def rewrite(payload: RewriteRequest):
231
+ try:
232
+ result = core_logic.rewrite_text(payload.text, payload.style, payload.api_key)
233
+ return ActionResponse(result=result)
234
+ except Exception as e:
235
+ raise HTTPException(status_code=500, detail=str(e))
236
+
237
+ @app.post("/key-points", response_model=ActionResponse)
238
+ def key_points(payload: TextRequest):
239
+ try:
240
+ result = core_logic.list_key_points(payload.text, payload.api_key)
241
+ return ActionResponse(result=result)
242
+ except Exception as e:
243
+ raise HTTPException(status_code=500, detail=str(e))
244
+
245
+ @app.post("/faqs", response_model=ActionResponse)
246
+ def faqs(payload: TextRequest):
247
+ try:
248
+ result = core_logic.generate_faqs(payload.text, payload.api_key)
249
+ return ActionResponse(result=result)
250
+ except Exception as e:
251
+ raise HTTPException(status_code=500, detail=str(e))
252
+
253
+ @app.post("/study-notes", response_model=ActionResponse)
254
+ def study_notes(payload: TextRequest):
255
+ try:
256
+ result = core_logic.create_study_notes(payload.text, payload.api_key)
257
+ return ActionResponse(result=result)
258
+ except Exception as e:
259
+ raise HTTPException(status_code=500, detail=str(e))
260
+
261
+ if __name__ == "__main__":
262
+ # pyrefly: if __name__ == "__main__":
263
+ import uvicorn
264
+ import os
265
+ port = int(os.environ.get("PORT", 8000))
266
+ uvicorn.run(app, host="0.0.0.0", port=port)