Yoon-gu Hwang commited on
Commit
af9d55b
·
1 Parent(s): 1314c33

multiple apis and ui in a mcp server

Browse files
Files changed (1) hide show
  1. app.py +72 -26
app.py CHANGED
@@ -1,37 +1,83 @@
1
- import json
2
  import gradio as gr
3
- from textblob import TextBlob
 
 
4
 
5
- def sentiment_analysis(text: str) -> str:
6
  """
7
- Analyze the sentiment of the given text.
8
 
9
  Args:
10
- text (str): The text to analyze
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  Returns:
13
- str: A JSON string containing polarity, subjectivity, and assessment
14
  """
15
- blob = TextBlob(text)
16
- sentiment = blob.sentiment
17
-
18
- result = {
19
- "polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
20
- "subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
21
- "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
22
- }
23
-
24
- return json.dumps(result)
25
-
26
- # Create the Gradio interface
27
- demo = gr.Interface(
28
- fn=sentiment_analysis,
29
- inputs=gr.Textbox(placeholder="Enter text to analyze..."),
30
- outputs=gr.Textbox(), # Changed from gr.JSON() to gr.Textbox()
31
- title="Text Sentiment Analysis",
32
- description="Analyze the sentiment of text using TextBlob"
 
 
 
 
33
  )
34
 
35
- # Launch the interface and MCP server
36
  if __name__ == "__main__":
37
- demo.launch(mcp_server=True, share=True)
 
1
+ import numpy as np
2
  import gradio as gr
3
+ from pathlib import Path
4
+ import os
5
+ from PIL import Image
6
 
7
+ def prime_factors(n: str):
8
  """
9
+ Compute the prime factorization of a positive integer.
10
 
11
  Args:
12
+ n (str): The integer to factorize. Must be greater than 1.
13
+ """
14
+ n_int = int(n)
15
+ if n_int <= 1:
16
+ raise ValueError("Input must be an integer greater than 1.")
17
+
18
+ factors = []
19
+ while n_int % 2 == 0:
20
+ factors.append(2)
21
+ n_int //= 2
22
+
23
+ divisor = 3
24
+ while divisor * divisor <= n_int:
25
+ while n_int % divisor == 0:
26
+ factors.append(divisor)
27
+ n_int //= divisor
28
+ divisor += 2
29
+
30
+ if n_int > 1:
31
+ factors.append(n_int)
32
+
33
+ return factors
34
+
35
+ def image_orientation(image: Image.Image) -> str:
36
+ """
37
+ Returns whether image is portrait or landscape.
38
+
39
+ Args:
40
+ image (Image.Image): The image to check.
41
+
42
+ Returns:
43
+ str: "Portrait" if image is portrait, "Landscape" if image is landscape.
44
+ """
45
+ return "Portrait" if image.height > image.width else "Landscape"
46
+
47
+
48
+ def sepia(input_img):
49
+ """
50
+ Apply a sepia filter to the input image.
51
+
52
+ Args:
53
+ input_img (np.array): The input image to apply the sepia filter to.
54
 
55
  Returns:
56
+ The sepia filtered image.
57
  """
58
+ sepia_filter = np.array([
59
+ [0.393, 0.769, 0.189],
60
+ [0.349, 0.686, 0.168],
61
+ [0.272, 0.534, 0.131]
62
+ ])
63
+ sepia_img = input_img.dot(sepia_filter.T)
64
+ sepia_img /= sepia_img.max()
65
+ return sepia_img
66
+
67
+
68
+
69
+ demo = gr.TabbedInterface(
70
+ [
71
+ gr.Interface(prime_factors, gr.Textbox("1001"), gr.Textbox()),
72
+ gr.Interface(image_orientation, gr.Image(type="pil"), gr.Textbox()),
73
+ gr.Interface(sepia, gr.Image(), gr.Image()),
74
+ ],
75
+ [
76
+ "Prime Factors",
77
+ "Image Orientation Checker",
78
+ "Sepia Filter",
79
+ ]
80
  )
81
 
 
82
  if __name__ == "__main__":
83
+ demo.launch(mcp_server=True)