Aadityaramrame commited on
Commit
fb7afb2
·
verified ·
1 Parent(s): 1b1fea4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -31
app.py CHANGED
@@ -1,42 +1,22 @@
1
  import gradio as gr
2
- import requests
3
 
4
- # -------------------------------
5
- # CONFIG
6
- # -------------------------------
7
- API_URL = "https://carecompanion-keywordextraction.onrender.com/extract_keywords" # replace with your actual endpoint
8
 
9
- # -------------------------------
10
- # FUNCTION TO CALL RENDER API
11
- # -------------------------------
12
  def extract_keywords(text):
13
- try:
14
- payload = {"text": text}
15
- response = requests.post(API_URL, json=payload)
 
16
 
17
- if response.status_code == 200:
18
- data = response.json()
19
- # Customize this based on your API response format
20
- return data.get("keywords", "No keywords found.")
21
- else:
22
- return f"⚠️ API Error {response.status_code}: {response.text}"
23
- except Exception as e:
24
- return f"❌ Connection error: {str(e)}"
25
-
26
- # -------------------------------
27
- # GRADIO INTERFACE
28
- # -------------------------------
29
  demo = gr.Interface(
30
  fn=extract_keywords,
31
- inputs=gr.Textbox(label="Enter text for keyword extraction", placeholder="Paste your text here..."),
32
- outputs=gr.Textbox(label="Extracted Keywords"),
33
- title="🔍 Keyword Extraction API Demo",
34
- description="This Gradio interface calls a deployed keyword extraction API hosted on Render.",
35
- theme="soft"
36
  )
37
 
38
- # -------------------------------
39
- # LAUNCH
40
- # -------------------------------
41
  if __name__ == "__main__":
42
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # Load the Hugging Face model for keyword/keyphrase extraction
5
+ extractor = pipeline("text2text-generation", model="ml6team/keyphrase-extraction-distilbert-inspec")
 
 
6
 
 
 
 
7
  def extract_keywords(text):
8
+ if not text.strip():
9
+ return "⚠️ Please enter some text."
10
+ result = extractor(text, max_length=64, clean_up_tokenization_spaces=True)
11
+ return result[0]['generated_text']
12
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  demo = gr.Interface(
14
  fn=extract_keywords,
15
+ inputs=gr.Textbox(lines=6, placeholder="Enter medical or general text here..."),
16
+ outputs="text",
17
+ title="🔍 Keyword Extraction",
18
+ description="Extract key phrases and keywords using a Hugging Face Transformer model."
 
19
  )
20
 
 
 
 
21
  if __name__ == "__main__":
22
  demo.launch()