Huzaifa367 commited on
Commit
c9f9208
·
verified ·
1 Parent(s): c76b20b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -23
app.py CHANGED
@@ -1,38 +1,27 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
4
- from threading import Thread
5
  import os
6
  import requests
7
 
8
-
9
- class StopOnTokens(StoppingCriteria):
10
- def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
11
- stop_ids = [29, 0]
12
- for stop_id in stop_ids:
13
- if input_ids[0][-1] == stop_id:
14
- return True
15
- return False
16
 
17
  def predict(message, history):
 
18
  history_transformer_format = history + [[message, ""]]
19
- stop = StopOnTokens()
20
-
21
- messages = "".join(["".join(["\n<human>:"+item[0], "\n<bot>:"+item[1]])
22
- for item in history_transformer_format])
23
 
24
- # Use your API to generate the response
25
  try:
26
- API_URL = "https://api-inference.huggingface.co/models/Huzaifa367/chat-summarizer"
27
- API_TOKEN = os.getenv("AUTH_TOKEN")
28
- HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
29
  payload = {"inputs": messages}
30
-
31
  response = requests.post(API_URL, headers=HEADERS, json=payload)
32
- response.raise_for_status() # Raise exception for non-2xx status codes
33
- summary_text = response.json()[0]["summary_text"] if isinstance(response, list) else response.get("summary_text", "")
 
 
34
  except requests.exceptions.RequestException as e:
35
- print(f"Error querying Hugging Face API: {e}")
36
  summary_text = f"Error querying Hugging Face API: {e}"
37
 
38
  return summary_text
 
1
  import gradio as gr
 
 
 
2
  import os
3
  import requests
4
 
5
+ # Define the Hugging Face API details
6
+ API_URL = "https://api-inference.huggingface.co/models/Huzaifa367/chat-summarizer"
7
+ API_TOKEN = os.getenv("AUTH_TOKEN")
8
+ HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
 
 
 
 
9
 
10
  def predict(message, history):
11
+ # Prepare input data for the Hugging Face API
12
  history_transformer_format = history + [[message, ""]]
13
+ messages = "".join([f"<human>:{item[0]}\n<bot>:{item[1]}" for item in history_transformer_format])
 
 
 
14
 
 
15
  try:
16
+ # Send request to the Hugging Face API
 
 
17
  payload = {"inputs": messages}
 
18
  response = requests.post(API_URL, headers=HEADERS, json=payload)
19
+ response.raise_for_status() # Raise exception for non-2xx status codes
20
+
21
+ # Extract summary text from the response
22
+ summary_text = response.json()["summary_text"]
23
  except requests.exceptions.RequestException as e:
24
+ # Handle API request errors
25
  summary_text = f"Error querying Hugging Face API: {e}"
26
 
27
  return summary_text