DeMaking commited on
Commit
0da0126
verified
1 Parent(s): aab4791

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -3,19 +3,21 @@ import os
3
  from fastapi import FastAPI, Request
4
  from contextlib import asynccontextmanager
5
  from transformers import pipeline
6
- from langdetect import detect # to change to langid
7
  import langid
8
  from huggingface_hub import login
9
  import socket
10
 
 
11
  # Global variables
12
  HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
13
 
 
14
  # Verify Hugging Face token
15
  if not HF_HUB_TOKEN:
16
  raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN in environment variables.")
17
  login(token=HF_HUB_TOKEN)
18
 
 
19
  # Load Hebrew and English text generation models
20
  hebrew_generator = pipeline("text-generation", model="Norod78/hebrew-gpt_neo-small")
21
  english_generator = pipeline("text-generation", model="distilgpt2")
@@ -25,10 +27,10 @@ english_generator = pipeline("text-generation", model="distilgpt2")
25
  def detect_language(user_input):
26
  try:
27
  # lang = detect(user_input)
28
- lang = langid.classify(user_input)
29
  print(f"Detected language: {lang}")
30
  return "hebrew" if lang == "he" else "english" if lang == "en" else "unsupported"
31
- except:
32
  print(f"Language detection error: {e}")
33
  return "unsupported"
34
 
@@ -64,7 +66,7 @@ async def root():
64
  async def generate_text(request: Request):
65
  try:
66
  data = await request.json()
67
- text = data.get("text", "").strip() # removes non relevant spaces
68
 
69
  if not text:
70
  return {"error": "No text provided"}
 
3
  from fastapi import FastAPI, Request
4
  from contextlib import asynccontextmanager
5
  from transformers import pipeline
 
6
  import langid
7
  from huggingface_hub import login
8
  import socket
9
 
10
+
11
  # Global variables
12
  HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
13
 
14
+
15
  # Verify Hugging Face token
16
  if not HF_HUB_TOKEN:
17
  raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN in environment variables.")
18
  login(token=HF_HUB_TOKEN)
19
 
20
+
21
  # Load Hebrew and English text generation models
22
  hebrew_generator = pipeline("text-generation", model="Norod78/hebrew-gpt_neo-small")
23
  english_generator = pipeline("text-generation", model="distilgpt2")
 
27
  def detect_language(user_input):
28
  try:
29
  # lang = detect(user_input)
30
+ lang _ = langid.classify(user_input) # langid.classify returns a tuple (language, confidence)
31
  print(f"Detected language: {lang}")
32
  return "hebrew" if lang == "he" else "english" if lang == "en" else "unsupported"
33
+ except Exception as e:
34
  print(f"Language detection error: {e}")
35
  return "unsupported"
36
 
 
66
  async def generate_text(request: Request):
67
  try:
68
  data = await request.json()
69
+ text = data.get("text", "").strip() # removes non-relevant spaces
70
 
71
  if not text:
72
  return {"error": "No text provided"}