ZweliM commited on
Commit
f97d2f5
·
verified ·
1 Parent(s): a388aae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
app.py CHANGED
@@ -1,34 +1,44 @@
 
1
  import gradio as gr
2
- from playwright.sync_api import sync_playwright
3
 
4
- def scrape_title(url: str) -> str:
 
5
  """
6
- Scrape the <title> tag from a web page at the given URL.
7
 
8
  Args:
9
- url (str): The website URL to scrape.
 
10
  Returns:
11
- str: The page title or an error message.
12
  """
13
- try:
14
- with sync_playwright() as p:
15
- browser = p.chromium.launch(headless=True)
16
- page = browser.new_page()
17
- page.goto(url, timeout=15000)
18
- title = page.title()
19
- browser.close()
20
- return title
21
- except Exception as e:
22
- return f"Error: {str(e)}"
23
-
24
- # Build Gradio interface exposing the scrape_title tool
 
 
 
25
  demo = gr.Interface(
26
- fn=scrape_title,
27
- inputs=gr.Textbox(label="URL", placeholder="https://example.com"),
28
- outputs=gr.Textbox(label="Page Title"),
29
- title="Playwright Web Scraper",
30
- description="Enter a URL to scrape its <title> tag using Playwright.",
 
 
 
31
  )
32
 
 
33
  if __name__ == "__main__":
34
- demo.launch(mcp_server=True)
 
1
+ import json
2
  import gradio as gr
3
+ from textblob import TextBlob
4
 
5
+
6
+ def sentiment_analysis(text: str) -> str:
7
  """
8
+ Analyze the sentiment of the given text.
9
 
10
  Args:
11
+ text(str): The text to analyze the
12
+
13
  Returns:
14
+ str: A JSON string containing polarity, subjectivity, and assessment
15
  """
16
+
17
+ blob = TextBlob(text)
18
+ sentiment = blob.sentiment
19
+
20
+ result = {
21
+ "polarity": round(sentiment.polarity, 2), # -1 negative, 1 positive
22
+ # 0 objective, 1 subjective
23
+ "subjectivity": round(sentiment.subjectivity, 2),
24
+ "assessment": "Positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
25
+ }
26
+
27
+ return json.dumps(result)
28
+
29
+
30
+ # creating the Gradio interface
31
  demo = gr.Interface(
32
+ fn=sentiment_analysis,
33
+ inputs=gr.Textbox(label="Enter text for sentiment analysis",
34
+ placeholder="Type here..."),
35
+ outputs=gr.Textbox(label="Sentiment Analysis Result",
36
+ placeholder="Result will be displayed here..."),
37
+ title="Sentiment Analysis",
38
+ description="This app analyzes the sentiment of the input text using TextBlob and returns the polarity, subjectivity, and assessment.",
39
+ theme="default"
40
  )
41
 
42
+ # launching the Gradio app
43
  if __name__ == "__main__":
44
+ demo.launch(mcp_server=True)