liang-huggingface commited on
Commit
9183e0b
·
1 Parent(s): d49a7e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -4
app.py CHANGED
@@ -6,6 +6,40 @@ import requests
6
  import os
7
 
8
  HF_API = os.getenv('HF_API')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  # Function to search PubMed for articles
10
  def search_pubmed(query, retmax):
11
  Entrez.email = '[email protected]'
@@ -29,7 +63,7 @@ def search_pubmed(query, retmax):
29
  return pd.DataFrame(article_list)
30
 
31
  # Function to summarize articles using Hugging Face's API
32
- def summarize_with_huggingface(model, selected_articles):
33
  API_URL = f"https://api-inference.huggingface.co/models/{model}"
34
  # Your Hugging Face API key
35
  API_KEY = HF_API
@@ -46,9 +80,13 @@ def summarize_with_huggingface(model, selected_articles):
46
  "inputs": text_to_summarize,
47
  "parameters": {"max_length": 300} # Adjust as needed
48
  }
49
- # Make the POST request to the Hugging Face API
50
- response = requests.post(API_URL, headers=headers, json=payload)
51
- response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
 
 
 
 
52
  # The API returns a list of dictionaries. We extract the summary from the first one.
53
  return response.json()[0]['generated_text']
54
 
 
6
  import os
7
 
8
  HF_API = os.getenv('HF_API')
9
+
10
+ from transformers import AutoModelForCausalLM, AutoTokenizer
11
+ import torch
12
+
13
+ # Load the model and tokenizer
14
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B-Chat")
15
+ model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-7B-Chat")
16
+
17
+ def generate_summary(prompt):
18
+ # Add instructions to the prompt to signal that you want a summary
19
+ instructions = "Summarize the following text:"
20
+ prompt_with_instructions = f"{instructions}\n{prompt}"
21
+
22
+ # Tokenize the prompt text and return PyTorch tensors
23
+ inputs = tokenizer.encode(prompt_with_instructions, return_tensors="pt")
24
+
25
+ # Generate a response using the model
26
+ outputs = model.generate(inputs, max_length=512, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
27
+
28
+ # Decode the response
29
+ summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
30
+ return summary
31
+
32
+ def generate_response(prompt):
33
+ # Tokenize the prompt text and return PyTorch tensors
34
+ inputs = tokenizer.encode(prompt, return_tensors="pt")
35
+
36
+ # Generate a response using the model
37
+ outputs = model.generate(inputs, max_length=512, num_return_sequences=1)
38
+
39
+ # Decode the response
40
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
41
+ return response
42
+
43
  # Function to search PubMed for articles
44
  def search_pubmed(query, retmax):
45
  Entrez.email = '[email protected]'
 
63
  return pd.DataFrame(article_list)
64
 
65
  # Function to summarize articles using Hugging Face's API
66
+ def summarize_with_huggingface(model, selected_articles, USE_LOCAL=True):
67
  API_URL = f"https://api-inference.huggingface.co/models/{model}"
68
  # Your Hugging Face API key
69
  API_KEY = HF_API
 
80
  "inputs": text_to_summarize,
81
  "parameters": {"max_length": 300} # Adjust as needed
82
  }
83
+
84
+ if USE_LOCAL:
85
+ response = generate_response(text_to_summarize)
86
+ else:
87
+ # Make the POST request to the Hugging Face API
88
+ response = requests.post(API_URL, headers=headers, json=payload)
89
+ response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
90
  # The API returns a list of dictionaries. We extract the summary from the first one.
91
  return response.json()[0]['generated_text']
92