File size: 7,727 Bytes
19737a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import gradio as gr
import os
import requests
from huggingface_hub import InferenceClient
import google.generativeai as genai
import openai

def api_check_msg(api_key, selected_model):
    res = validate_api_key(api_key, selected_model)
    return res["message"]

def validate_api_key(api_key, selected_model):
    # Check if the API key is valid for GPT-3.5-Turbo
    if "GPT" in selected_model:
        url = "https://api.openai.com/v1/models"
        headers = {
            "Authorization": f"Bearer {api_key}"
        }
        try:
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                return {"is_valid": True, "message": '<p style="color: green;">API Key is valid!</p>'}
            else:
                return {"is_valid": False, "message": f'<p style="color: red;">Invalid OpenAI API Key. Status code: {response.status_code}</p>'}
        except requests.exceptions.RequestException as e:
            return {"is_valid": False, "message": f'<p style="color: red;">Invalid OpenAI API Key. Error: {e}</p>'}
    elif "Llama" in selected_model:
        url = "https://huggingface.co/api/whoami-v2"
        headers = {
            "Authorization": f"Bearer {api_key}"
        }
        try:
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                return {"is_valid": True, "message": '<p style="color: green;">API Key is valid!</p>'}
            else:
                return {"is_valid": False, "message": f'<p style="color: red;">Invalid Hugging Face API Key. Status code: {response.status_code}</p>'}
        except requests.exceptions.RequestException as e:
            return {"is_valid": False, "message": f'<p style="color: red;">Invalid Hugging Face API Key. Error: {e}</p>'}
    elif "Gemini" in selected_model:
        try:
            genai.configure(api_key=api_key)
            model = genai.GenerativeModel("gemini-1.5-flash")
            response = model.generate_content("Help me diagnose the patient.")
            return {"is_valid": True, "message": '<p style="color: green;">API Key is valid!</p>'}
        except Exception as e:
            return {"is_valid": False, "message": f'<p style="color: red;">Invalid Google API Key. Error: {e}</p>'}

def generate_text_chatgpt(key, prompt, temperature, top_p):

    openai.api_key = key

    response = openai.chat.completions.create(
      model="gpt-4-0613",
      messages=[{"role": "system", "content": "You are a talented diagnostician who is diagnosing a patient."},
                {"role": "user", "content": prompt}],
      temperature=temperature,
      max_tokens=50,
      top_p=top_p,
      frequency_penalty=0
    )

    return response.choices[0].message.content


def generate_text_gemini(key, prompt, temperature, top_p):
    genai.configure(api_key=key)

    generation_config = genai.GenerationConfig(
        max_output_tokens=len(prompt)+50,
        temperature=temperature,
        top_p=top_p,
    )
    model = genai.GenerativeModel("gemini-1.5-flash", generation_config=generation_config)
    response = model.generate_content(prompt)
    return response.text


def generate_text_llama(key, prompt, temperature, top_p):
    model_name = "meta-llama/Meta-Llama-3-8B-Instruct"
    client = InferenceClient(api_key=key)

    messages = [{"role": "system", "content": "You are a talented diagnostician who is diagnosing a patient."},
                {"role": "user","content": prompt}]

    completion = client.chat.completions.create(
        model=model_name,
        messages=messages, 
        max_tokens=len(prompt)+50,
        temperature=temperature,
        top_p=top_p
    )

    response = completion.choices[0].message.content
    if len(response) > len(prompt):
        return response[len(prompt):]
    return response


def diagnose(key, model, top_k, temperature, symptom_prompt):

    model_map = {
        "GPT-3.5-Turbo": "GPT",
        "Llama-3": "Llama",
        "Gemini-1.5": "Gemini"
    }
    if symptom_prompt:
        if "GPT" in model:
            message = generate_text_chatgpt(key, symptom_prompt, temperature, top_k)
        elif "Llama" in model:
            message = generate_text_llama(key, symptom_prompt, temperature, top_k)
        elif "Gemini" in model:
            message = generate_text_gemini(key, symptom_prompt, temperature, top_k)
        else:
            message = "Incorrect model, please try again."
    else:
        message = "Please add the symptoms data"
    
    return message

def update_model_components(selected_model):
    model_map = {
                "GPT-3.5-Turbo": "GPT",
                "Llama-3": "Llama",
                "Gemini-1.5": "Gemini"
            }

    link_map = {
        "GPT-3.5-Turbo": "https://platform.openai.com/account/api-keys",
        "Llama-3": "https://hf.co/settings/tokens",
        "Gemini-1.5": "https://aistudio.google.com/apikey"
    }
    textbox_label = f"Please input the API key for your {model_map[selected_model]} model"
    button_value = f"Don't have an API key? Get one for the {model_map[selected_model]} model here."
    button_link = link_map[selected_model]
    return gr.update(label=textbox_label), gr.update(value=button_value, link=button_link)

def toggle_button(symptoms_text, api_key, model):
    if symptoms_text.strip() and validate_api_key(api_key, model):
        return gr.update(interactive=True)
    return gr.update(interactive=False)


with gr.Blocks() as ui:

    with gr.Row(equal_height=500):
        with gr.Column(scale=1, min_width=300):
            model = gr.Radio(label="LLM Selection", value="GPT-3.5-Turbo",  
                             choices=["GPT-3.5-Turbo", "Llama-3", "Gemini-1.5"])
            is_valid = False
            key = gr.Textbox(label="Please input the API key for your GPT model", type="password")
            status_message = gr.HTML(label="Validation Status")
            key.input(fn=api_check_msg, inputs=[key, model], outputs=status_message)
            button = gr.Button(value="Don't have an API key? Get one for the GPT model here.", link="https://platform.openai.com/account/api-keys")
            model.change(update_model_components, inputs=model, outputs=[key, button])
            # gr.Button(value="OpenAi Key", link="https://platform.openai.com/account/api-keys")
            # gr.Button(value="Meta Llama Key", link="https://platform.openai.com/account/api-keys")
            # gr.Button(value="Gemini Key", link="https://platform.openai.com/account/api-keys")
            gr.ClearButton(key, variant="primary")
            
        with gr.Column(scale=2, min_width=600):
            gr.Markdown("## Hello, Welcome to the GUI by Team #9.")
            temperature = gr.Slider(0.0, 1.0, value=0.7, step = 0.05, label="Temperature", info="Set the Temperature")
            top_p = gr.Slider(0.0, 1.0, value=0.9, step = 0.05, label="top-p value", info="Set the sampling nucleus parameter")
            symptoms = gr.Textbox(label="Add the symptom data in the input to receive diagnosis")
            llm_btn = gr.Button(value="Diagnose Disease", variant="primary", elem_id="diagnose", interactive=False)
            symptoms.input(toggle_button, inputs=[symptoms, key, model], outputs=llm_btn)
            key.input(toggle_button, inputs=[symptoms, key, model], outputs=llm_btn)
            model.change(toggle_button, inputs=[symptoms, key, model], outputs=llm_btn)
            output = gr.Textbox(label="LLM Output Status", interactive=False, placeholder="Output will appear here...")
            llm_btn.click(fn=diagnose, inputs=[key, model, top_p, temperature, symptoms], outputs=output, api_name="auditor")


ui.launch(share=True)