PLB commited on
Commit
56aaedd
1 Parent(s): fc918c5

Add application file

Browse files
Files changed (4) hide show
  1. .gitignore +2 -0
  2. README.md +3 -3
  3. app.py +94 -0
  4. requirements.txt +74 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .env
2
+ .venv/
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Phospho Multimodal
3
- emoji: 👁
4
- colorFrom: indigo
5
- colorTo: pink
6
  sdk: gradio
7
  sdk_version: 4.29.0
8
  app_file: app.py
 
1
  ---
2
  title: Phospho Multimodal
3
+ emoji: 🧪
4
+ colorFrom: gray
5
+ colorTo: green
6
  sdk: gradio
7
  sdk_version: 4.29.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import base64
3
+ import time
4
+ import os
5
+ from dotenv import load_dotenv
6
+ import requests
7
+
8
+
9
+ load_dotenv()
10
+
11
+ phospho_api_key = os.environ.get("PHOSPHO_API_KEY")
12
+
13
+ assert phospho_api_key, "Please set the PHOSPHO_API_KEY environment variable"
14
+
15
+
16
+ # Image to Base 64 Converter
17
+ def image_to_base64(image_path):
18
+ with open(image_path, "rb") as img:
19
+ encoded_string = base64.b64encode(img.read())
20
+ return encoded_string.decode("utf-8")
21
+
22
+
23
+ # Function that takes User Inputs and displays it on ChatUI
24
+ def query_message(history, txt, img):
25
+ if not img:
26
+ history += [(txt, None)]
27
+ return history
28
+ base64 = image_to_base64(img)
29
+ data_url = f"data:image/jpeg;base64,{base64}"
30
+ history += [(f"{txt} ![]({data_url})", None)]
31
+ return history
32
+
33
+
34
+ # Function that takes User Inputs, generates Response and displays on Chat UI
35
+ def llm_response(history, text, img):
36
+ if not img:
37
+ answer = f"Please provide an image, otherwise I cannot answer."
38
+ history += [(None, answer)]
39
+ return history
40
+
41
+ else:
42
+ url = "https://api.phospho.ai/v2/predict"
43
+ headers = {
44
+ "accept": "application/json",
45
+ "Authorization": f"Bearer {phospho_api_key}",
46
+ "Content-Type": "application/json",
47
+ }
48
+ data = {
49
+ "inputs": [
50
+ {
51
+ "text": text,
52
+ "image_url": image_to_base64(img),
53
+ "temperature": 0.2,
54
+ "top_p": 0.9,
55
+ "max_new_tokens": 100,
56
+ }
57
+ ],
58
+ "model": "phospho-multimodal",
59
+ }
60
+
61
+ response = requests.post(url, json=data, headers=headers)
62
+
63
+ # Check if the response is successful
64
+ if response.status_code != 200:
65
+ history += [
66
+ (None, "Sorry, I couldn't process the image. Please try again.")
67
+ ]
68
+ return history
69
+
70
+ else:
71
+ response = response.json()
72
+ print(response)
73
+ answer = response["predictions"][0]["description"]
74
+ history += [(None, answer)]
75
+ return history
76
+
77
+
78
+ # Interface Code
79
+ with gr.Blocks() as app:
80
+ with gr.Row():
81
+ image_box = gr.Image(type="filepath")
82
+
83
+ chatbot = gr.Chatbot(scale=2, height=750)
84
+ text_box = gr.Textbox(
85
+ placeholder="What is your question about the image?",
86
+ container=False,
87
+ )
88
+
89
+ btn = gr.Button("Submit")
90
+ clicked = btn.click(query_message, [chatbot, text_box, image_box], chatbot).then(
91
+ llm_response, [chatbot, text_box, image_box], chatbot
92
+ )
93
+ app.queue()
94
+ app.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ altair==5.3.0
3
+ annotated-types==0.6.0
4
+ anyio==4.3.0
5
+ attrs==23.2.0
6
+ certifi==2024.2.2
7
+ charset-normalizer==3.3.2
8
+ click==8.1.7
9
+ contourpy==1.2.1
10
+ cycler==0.12.1
11
+ dnspython==2.6.1
12
+ email_validator==2.1.1
13
+ fastapi==0.111.0
14
+ fastapi-cli==0.0.3
15
+ ffmpy==0.3.2
16
+ filelock==3.14.0
17
+ fonttools==4.51.0
18
+ fsspec==2024.3.1
19
+ gradio==4.29.0
20
+ gradio_client==0.16.1
21
+ h11==0.14.0
22
+ httpcore==1.0.5
23
+ httptools==0.6.1
24
+ httpx==0.27.0
25
+ huggingface-hub==0.23.0
26
+ idna==3.7
27
+ importlib_resources==6.4.0
28
+ Jinja2==3.1.4
29
+ jsonschema==4.22.0
30
+ jsonschema-specifications==2023.12.1
31
+ kiwisolver==1.4.5
32
+ markdown-it-py==3.0.0
33
+ MarkupSafe==2.1.5
34
+ matplotlib==3.8.4
35
+ mdurl==0.1.2
36
+ mypy==1.10.0
37
+ mypy-extensions==1.0.0
38
+ numpy==1.26.4
39
+ orjson==3.10.3
40
+ packaging==24.0
41
+ pandas==2.2.2
42
+ pillow==10.3.0
43
+ pydantic==2.7.1
44
+ pydantic_core==2.18.2
45
+ pydub==0.25.1
46
+ Pygments==2.18.0
47
+ pyparsing==3.1.2
48
+ python-dateutil==2.9.0.post0
49
+ python-dotenv==1.0.1
50
+ python-multipart==0.0.9
51
+ pytz==2024.1
52
+ PyYAML==6.0.1
53
+ referencing==0.35.1
54
+ requests==2.31.0
55
+ rich==13.7.1
56
+ rpds-py==0.18.1
57
+ ruff==0.4.3
58
+ semantic-version==2.10.0
59
+ shellingham==1.5.4
60
+ six==1.16.0
61
+ sniffio==1.3.1
62
+ starlette==0.37.2
63
+ tomlkit==0.12.0
64
+ toolz==0.12.1
65
+ tqdm==4.66.4
66
+ typer==0.12.3
67
+ typing_extensions==4.11.0
68
+ tzdata==2024.1
69
+ ujson==5.9.0
70
+ urllib3==2.2.1
71
+ uvicorn==0.29.0
72
+ uvloop==0.19.0
73
+ watchfiles==0.21.0
74
+ websockets==11.0.3