Ramaravind commited on
Commit
3ea3319
·
verified ·
1 Parent(s): ea6b076

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -1,9 +1,16 @@
1
  import gradio as gr
2
- import requests
3
  import os
4
 
5
  HF_API_TOKEN = os.getenv("HF_API_TOKEN")
6
  MODEL = "bigscience/bloom-560m" #"microsoft/Phi-3-mini-128k-instruct"
 
 
 
 
 
 
 
7
 
8
  # -------------------------
9
  # Core function: call HF API
@@ -15,18 +22,16 @@ def query_llm(tweet, mode):
15
  else:
16
  prompt += "Task: Is the above TEXT toxic? First clearly state your decision. Then, provide specific reason(s) for your decision. If there is more than one reason, provide them in a numbered list. Your reason(s) must be non-redundant and jointly sufficient to justify your decision. In other words, there should not be any internal or external information unused in your explanation."
17
 
18
- headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
19
- response = requests.post(
20
- f"https://api-inference.huggingface.co/models/{MODEL}",
21
- headers=headers,
22
- json={"inputs": prompt}
23
- )
24
  try:
25
- out = response.json()
26
- return out[0]["generated_text"] if isinstance(out, list) else str(out)
 
 
 
 
27
  except Exception as e:
28
- return f"Error: {str(e)}"
29
-
30
 
31
  # -------------------------
32
  # Preloaded tweets
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
  import os
4
 
5
  HF_API_TOKEN = os.getenv("HF_API_TOKEN")
6
  MODEL = "bigscience/bloom-560m" #"microsoft/Phi-3-mini-128k-instruct"
7
+ # Initialize the InferenceClient with the token and model.
8
+ # Using a lightweight model like google/flan-t5-small.
9
+ client = InferenceClient(
10
+ "google/flan-t5-small",
11
+ token=HF_TOKEN
12
+ )
13
+
14
 
15
  # -------------------------
16
  # Core function: call HF API
 
22
  else:
23
  prompt += "Task: Is the above TEXT toxic? First clearly state your decision. Then, provide specific reason(s) for your decision. If there is more than one reason, provide them in a numbered list. Your reason(s) must be non-redundant and jointly sufficient to justify your decision. In other words, there should not be any internal or external information unused in your explanation."
24
 
25
+ # Use the client to generate text from the full prompt.
 
 
 
 
 
26
  try:
27
+ response = client.text_generation(
28
+ prompt=prompt,
29
+ max_new_tokens=250, # Control the length of the generated output.
30
+ stream=False, # Set to True for streaming responses.
31
+ )
32
+ return response
33
  except Exception as e:
34
+ return f"Error: {e}"
 
35
 
36
  # -------------------------
37
  # Preloaded tweets