Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,24 @@
|
|
1 |
-
import uvicorn
|
2 |
import logging
|
3 |
import os
|
4 |
from fastapi import FastAPI, Request
|
5 |
-
import
|
6 |
from transformers import pipeline
|
7 |
from langdetect import detect
|
8 |
from huggingface_hub import login
|
9 |
-
|
10 |
-
|
11 |
-
app = FastAPI()
|
12 |
|
13 |
# Global variables
|
14 |
HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
15 |
|
16 |
-
|
17 |
# Verify Hugging Face token
|
18 |
if not HF_HUB_TOKEN:
|
19 |
raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN in environment variables.")
|
20 |
login(token=HF_HUB_TOKEN)
|
21 |
|
22 |
-
|
23 |
-
# Configure logging
|
24 |
-
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
25 |
-
logger = logging.getLogger(__name__)
|
26 |
-
|
27 |
-
|
28 |
# Load Hebrew and English text generation models
|
29 |
hebrew_generator = pipeline("text-generation", model="Norod78/hebrew-gpt_neo-small")
|
30 |
english_generator = pipeline("text-generation", model="distilgpt2")
|
31 |
|
32 |
-
|
33 |
# Function to detect language
|
34 |
def detect_language(user_input):
|
35 |
try:
|
@@ -38,7 +27,6 @@ def detect_language(user_input):
|
|
38 |
except:
|
39 |
return "unsupported"
|
40 |
|
41 |
-
|
42 |
# Function to generate a response
|
43 |
def generate_response(text):
|
44 |
language = detect_language(text)
|
@@ -48,11 +36,31 @@ def generate_response(text):
|
|
48 |
return english_generator(text, max_length=100)[0]["generated_text"]
|
49 |
return "Sorry, I only support Hebrew and English."
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
@app.get("/")
|
53 |
async def root():
|
54 |
-
return {"message": "
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
|
|
57 |
if __name__ == "__main__":
|
58 |
-
|
|
|
|
|
|
1 |
import logging
|
2 |
import os
|
3 |
from fastapi import FastAPI, Request
|
4 |
+
from contextlib import asynccontextmanager
|
5 |
from transformers import pipeline
|
6 |
from langdetect import detect
|
7 |
from huggingface_hub import login
|
8 |
+
import socket
|
|
|
|
|
9 |
|
10 |
# Global variables
|
11 |
HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
12 |
|
|
|
13 |
# Verify Hugging Face token
|
14 |
if not HF_HUB_TOKEN:
|
15 |
raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN in environment variables.")
|
16 |
login(token=HF_HUB_TOKEN)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Load Hebrew and English text generation models
|
19 |
hebrew_generator = pipeline("text-generation", model="Norod78/hebrew-gpt_neo-small")
|
20 |
english_generator = pipeline("text-generation", model="distilgpt2")
|
21 |
|
|
|
22 |
# Function to detect language
|
23 |
def detect_language(user_input):
|
24 |
try:
|
|
|
27 |
except:
|
28 |
return "unsupported"
|
29 |
|
|
|
30 |
# Function to generate a response
|
31 |
def generate_response(text):
|
32 |
language = detect_language(text)
|
|
|
36 |
return english_generator(text, max_length=100)[0]["generated_text"]
|
37 |
return "Sorry, I only support Hebrew and English."
|
38 |
|
39 |
+
# FastAPI lifespan event
|
40 |
+
@asynccontextmanager
|
41 |
+
async def lifespan(app: FastAPI):
|
42 |
+
print("Starting application...")
|
43 |
+
yield # Wait until app closes
|
44 |
+
print("Shutting down application...")
|
45 |
+
|
46 |
+
# Create FastAPI app
|
47 |
+
app = FastAPI(lifespan=lifespan)
|
48 |
|
49 |
@app.get("/")
|
50 |
async def root():
|
51 |
+
return {"message": "Decision Helper API is running!"}
|
52 |
+
|
53 |
+
@app.post("/generate_response")
|
54 |
+
async def generate_text(request: Request):
|
55 |
+
data = await request.json()
|
56 |
+
text = data.get("text", "")
|
57 |
+
if not text:
|
58 |
+
return {"error": "No text provided"}
|
59 |
+
|
60 |
+
response = generate_response(text)
|
61 |
+
return {"response": response}
|
62 |
|
63 |
+
# Run the server
|
64 |
if __name__ == "__main__":
|
65 |
+
import uvicorn
|
66 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|