DeMaking commited on
Commit
baeb76a
·
verified ·
1 Parent(s): bec9f75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -8
app.py CHANGED
@@ -5,7 +5,7 @@ from fastapi import FastAPI, Request
5
  from contextlib import asynccontextmanager
6
  from transformers import pipeline
7
  import langid
8
- from huggingface_hub import login
9
  import socket
10
  import time
11
 
@@ -20,9 +20,10 @@ if not HF_HUB_TOKEN:
20
  raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN in environment variables.")
21
  login(token=HF_HUB_TOKEN)
22
 
 
23
 
24
  # Load Hebrew and English text generation models
25
- lang_generator = pipeline("text-generation", model="microsoft/Phi-3.5-mini-instruct")
26
 
27
 
28
  # Function to detect language
@@ -39,12 +40,34 @@ def detect_language(user_input):
39
  def generate_response(text):
40
  language = detect_language(text)
41
  print(f"Detected language: {language}, ", f"current time: {current_time_gmt()}")
42
-
 
 
 
43
  if language == "hebrew" or language == "english":
44
- # hebrew_generator = pipeline("text-generation", model="onlplab/alephbert-base")
45
- output = lang_generator(text, max_length=100, truncation=True)
46
- print(f"Model output: {output}, ", f"current time: {current_time_gmt()}") # Debugging
47
- return output[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  # elif language == "english":
50
  # #english_generator = pipeline("text-generation", model="mistralai/Mistral-Nemo-Instruct-2407", max_new_tokens=128)
@@ -53,7 +76,7 @@ def generate_response(text):
53
  # print(f"English model output: {output}, ", f"current time: {current_time_gmt()}") # Debugging
54
  # return output[0]["generated_text"]
55
 
56
- return "Sorry, I only support Hebrew and English."
57
 
58
 
59
  # FastAPI lifespan event
 
5
  from contextlib import asynccontextmanager
6
  from transformers import pipeline
7
  import langid
8
+ from huggingface_hub import InferenceClient, login
9
  import socket
10
  import time
11
 
 
20
  raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN in environment variables.")
21
  login(token=HF_HUB_TOKEN)
22
 
23
+ client = InferenceClient(api_key=HF_HUB_TOKEN)
24
 
25
  # Load Hebrew and English text generation models
26
+ # lang_generator = pipeline("text-generation", model="microsoft/Phi-3.5-mini-instruct")
27
 
28
 
29
  # Function to detect language
 
40
  def generate_response(text):
41
  language = detect_language(text)
42
  print(f"Detected language: {language}, ", f"current time: {current_time_gmt()}")
43
+
44
+ #template = "Use the text in the question as context to answer the question at the end.\
45
+ #If you don't know the answer, just say that you don't know, don't try to make up an answer."
46
+
47
  if language == "hebrew" or language == "english":
48
+ messages = [
49
+ { "role": "user", "content": text }
50
+ ]
51
+ print(f"Messages: {messages}, ", f"current time: {current_time_gmt()}")
52
+
53
+ completion = client.chat.completions.create(
54
+ model="microsoft/Phi-3.5-mini-instruct",
55
+ messages=messages,
56
+ max_tokens=400,
57
+ temperature=0.5,
58
+ top_p=0.7
59
+ )
60
+ print("\ncompletion: ", completion.choices[0].message, f"\n current time: {current_time_gmt()}")
61
+
62
+ return "Sorry, I only support Hebrew and English."
63
+
64
+
65
+
66
+ # if language == "hebrew" or language == "english":
67
+ # # hebrew_generator = pipeline("text-generation", model="onlplab/alephbert-base")
68
+ # output = lang_generator(text, max_length=250, truncation=True)
69
+ # print(f"Model output: {output}, ", f"current time: {current_time_gmt()}") # Debugging
70
+ # return output[0]["generated_text"]
71
 
72
  # elif language == "english":
73
  # #english_generator = pipeline("text-generation", model="mistralai/Mistral-Nemo-Instruct-2407", max_new_tokens=128)
 
76
  # print(f"English model output: {output}, ", f"current time: {current_time_gmt()}") # Debugging
77
  # return output[0]["generated_text"]
78
 
79
+ # return "Sorry, I only support Hebrew and English."
80
 
81
 
82
  # FastAPI lifespan event