Aadityaramrame commited on
Commit
4ff5d1d
·
verified ·
1 Parent(s): 75acde5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -1,29 +1,42 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- # Publicly available summarization model
5
- extractor = pipeline("summarization", model="facebook/bart-large-cnn")
6
 
7
- def extract_keywords(text):
8
  if not text.strip():
9
  return "⚠️ Please enter some text."
 
10
  try:
11
- result = extractor(text, max_length=40, min_length=5, do_sample=False)
12
- keywords = result[0]['summary_text']
13
- return f"🧠 **Extracted Keywords (Approx):**\n\n{keywords}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  except Exception as e:
15
- return f" Error: {str(e)}"
16
 
17
  demo = gr.Interface(
18
- fn=extract_keywords,
19
- inputs=gr.Textbox(
20
- label="📝 Enter Text",
21
- placeholder="Paste your text here...",
22
- lines=6
23
- ),
24
- outputs=gr.Markdown(label="📊 Extracted Keywords"),
25
- title="🔍 Keyword Extractor (using BART)",
26
- description="A quick and lightweight keyword extractor built using `facebook/bart-large-cnn` summarization model.",
27
  theme="soft"
28
  )
29
 
 
1
  import gradio as gr
2
+ import requests
3
 
4
+ API_URL = "https://carecompanion-keywordextraction.onrender.com/extract_keywords"
 
5
 
6
+ def extract_keywords_from_api(text):
7
  if not text.strip():
8
  return "⚠️ Please enter some text."
9
+
10
  try:
11
+ response = requests.post(API_URL, json={"text": text})
12
+ if response.status_code != 200:
13
+ return f" API Error: {response.status_code}"
14
+
15
+ data = response.json()
16
+
17
+ # Format output nicely
18
+ output = ""
19
+ for category, items in data.items():
20
+ if items:
21
+ output += f"### 🩸 {category.capitalize()}\n"
22
+ for item in items:
23
+ if isinstance(item, list):
24
+ output += f"- {item[0]} (score: {item[1]:.2f})\n"
25
+ else:
26
+ output += f"- {item}\n"
27
+ output += "\n"
28
+
29
+ return output if output else "No keywords found."
30
+
31
  except Exception as e:
32
+ return f"⚠️ Error: {str(e)}"
33
 
34
  demo = gr.Interface(
35
+ fn=extract_keywords_from_api,
36
+ inputs=gr.Textbox(label="Enter medical text to extract keywords:", lines=5, placeholder="Example: The patient is prescribed amoxicillin and paracetamol for fever."),
37
+ outputs=gr.Markdown(label="Extracted Keywords"),
38
+ title="🧠 Keyword Extraction from Medical Text",
39
+ description="Calls the Render API to extract structured medical keywords.",
 
 
 
 
40
  theme="soft"
41
  )
42