Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
from openai import OpenAI
|
|
|
4 |
import os
|
5 |
import io
|
6 |
import base64
|
@@ -8,8 +9,9 @@ import base64
|
|
8 |
# Set API key
|
9 |
#base_url = os.environ.get("OPENAI_API_BASE")
|
10 |
api_key = ""
|
11 |
-
client = OpenAI(api_key=api_key)
|
12 |
|
|
|
|
|
13 |
# Define the model to be used
|
14 |
MODEL = os.environ.get("MODEL")
|
15 |
|
@@ -36,6 +38,17 @@ footer {
|
|
36 |
|
37 |
LICENSE = 'MODEL: ' + MODEL + ' LOADED'
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def read(filename):
|
40 |
with open(filename) as f:
|
41 |
data = f.read()
|
@@ -44,7 +57,8 @@ def read(filename):
|
|
44 |
SYS_PROMPT = read('system_prompt.txt')
|
45 |
|
46 |
def process_text(text_input, unit):
|
47 |
-
if text_input:
|
|
|
48 |
completion = client.chat.completions.create(
|
49 |
model=MODEL,
|
50 |
messages=[
|
@@ -53,6 +67,10 @@ def process_text(text_input, unit):
|
|
53 |
]
|
54 |
)
|
55 |
return completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
56 |
return ""
|
57 |
|
58 |
def encode_image_to_base64(image_input):
|
@@ -62,7 +80,8 @@ def encode_image_to_base64(image_input):
|
|
62 |
return img_str
|
63 |
|
64 |
def process_image(image_input, unit):
|
65 |
-
if image_input is not None:
|
|
|
66 |
#with open(image_input.name, "rb") as f:
|
67 |
# base64_image = base64.b64encode(f.read()).decode("utf-8")
|
68 |
base64_image = encode_image_to_base64(image_input)
|
@@ -83,6 +102,10 @@ def process_image(image_input, unit):
|
|
83 |
max_tokens=1024,
|
84 |
)
|
85 |
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
86 |
|
87 |
|
88 |
def main(text_input="", image_input=None, unit=""):
|
@@ -102,7 +125,7 @@ with gr.Blocks(theme='shivi/calm_seafoam', css=css, title="Medster - Medical Dia
|
|
102 |
with gr.Row():
|
103 |
output_box = gr.Markdown(label="Diagnosis") # Create an output textbox
|
104 |
with gr.Row():
|
105 |
-
api_key = gr.Textbox(label="
|
106 |
with gr.Row():
|
107 |
image_input = gr.Image(type="pil", label="Upload Image") # Create an image upload button
|
108 |
text_input = gr.Textbox(label="Submit") # Create a text input box
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
from openai import OpenAI
|
4 |
+
import google.generativeai as genai
|
5 |
import os
|
6 |
import io
|
7 |
import base64
|
|
|
9 |
# Set API key
|
10 |
#base_url = os.environ.get("OPENAI_API_BASE")
|
11 |
api_key = ""
|
|
|
12 |
|
13 |
+
endpoints = ''
|
14 |
+
|
15 |
# Define the model to be used
|
16 |
MODEL = os.environ.get("MODEL")
|
17 |
|
|
|
38 |
|
39 |
LICENSE = 'MODEL: ' + MODEL + ' LOADED'
|
40 |
|
41 |
+
|
42 |
+
def endpoints(api_key):
|
43 |
+
if api_key not None:
|
44 |
+
if api_key[:3] == "sk-":
|
45 |
+
return 'OPENAI'
|
46 |
+
else:
|
47 |
+
return 'GOOGLE'
|
48 |
+
return
|
49 |
+
|
50 |
+
endpoints = endpoints(api_key)
|
51 |
+
|
52 |
def read(filename):
|
53 |
with open(filename) as f:
|
54 |
data = f.read()
|
|
|
57 |
SYS_PROMPT = read('system_prompt.txt')
|
58 |
|
59 |
def process_text(text_input, unit):
|
60 |
+
if text_input and endpoints == 'OPENAI':
|
61 |
+
client = OpenAI(api_key=api_key)
|
62 |
completion = client.chat.completions.create(
|
63 |
model=MODEL,
|
64 |
messages=[
|
|
|
67 |
]
|
68 |
)
|
69 |
return completion.choices[0].message.content
|
70 |
+
elif text_input and endpoints == "GOOGLE":
|
71 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
72 |
+
model = genai.GenerativeModel(MODEL),
|
73 |
+
return response = model.generate_content(SYS_PROMPT + text_input).text
|
74 |
return ""
|
75 |
|
76 |
def encode_image_to_base64(image_input):
|
|
|
80 |
return img_str
|
81 |
|
82 |
def process_image(image_input, unit):
|
83 |
+
if image_input is not None and endpoints == 'OPENAI':
|
84 |
+
client = OpenAI(api_key=api_key)
|
85 |
#with open(image_input.name, "rb") as f:
|
86 |
# base64_image = base64.b64encode(f.read()).decode("utf-8")
|
87 |
base64_image = encode_image_to_base64(image_input)
|
|
|
102 |
max_tokens=1024,
|
103 |
)
|
104 |
return response.choices[0].message.content
|
105 |
+
elif image_input is not None and ENDPOINTS == "GOOGLE":
|
106 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
107 |
+
model = genai.GenerativeModel(MODEL),
|
108 |
+
return response = model.generate_content(image_input).text
|
109 |
|
110 |
|
111 |
def main(text_input="", image_input=None, unit=""):
|
|
|
125 |
with gr.Row():
|
126 |
output_box = gr.Markdown(label="Diagnosis") # Create an output textbox
|
127 |
with gr.Row():
|
128 |
+
api_key = gr.Textbox(label="API Key") # Input API key
|
129 |
with gr.Row():
|
130 |
image_input = gr.Image(type="pil", label="Upload Image") # Create an image upload button
|
131 |
text_input = gr.Textbox(label="Submit") # Create a text input box
|