Download app.py from Wellvida/wellvida: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/spaces/Wellvida/wellvida/resolve/main/app.py
- Command line
-
hf download hf://spaces/Wellvida/wellvida/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Wellvida/wellvida/resolve/main/app.py
3.31 kB
| import os | |
| import openai | |
| import pandas as pd | |
| import gradio as gr | |
| # Recommendation function | |
| def get_recommendations( | |
| persona: str, problem: str, | |
| model: str = 'gpt-3.5-turbo', | |
| therapies: str = None, | |
| output_json: bool = True, | |
| max_tokens_output: int = 2024 | |
| ) -> str: | |
| openai_key = os.getenv("OPENAI_API_KEY") | |
| client = openai.OpenAI(api_key=openai_key) | |
| with open('system_prompt.txt') as f: | |
| system_prompt = f.read().strip() | |
| if therapies: | |
| system_prompt += '\nDie verfügbaren Therapien sind: ' + therapies | |
| if output_json: | |
| with open('json_prompt.txt') as f: | |
| json_prompt = f.read().strip() | |
| system_prompt += '\n' + json_prompt | |
| response_format = {"type": "json_object"} | |
| else: | |
| response_format = {"type": "text"} | |
| print('System prompt:', system_prompt) | |
| prompt_template = "# Person-Beschreibung #\n{}\n# Problembeschreibung #\n{}" | |
| prompt = prompt_template.format(persona, problem) | |
| messages = [ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| response = client.chat.completions.create( | |
| model=model, | |
| messages=messages, | |
| max_tokens=max_tokens_output, | |
| response_format=response_format, | |
| temperature=0, | |
| top_p=0, | |
| seed=42, | |
| ) | |
| # TODO convert response with json.loads() and then parse somehow to Markdown or similar? | |
| return response.choices[0].message.content | |
| # Gradio | |
| example_cases_df = pd.read_excel('20240513_Testdaten_Wellvida_version ms-Update.xlsx', header=2) | |
| example_cases_df.dropna(inplace=True) | |
| personas = [ | |
| '\n'.join(x) for x in | |
| zip(example_cases_df['Name (fiktiv)'], example_cases_df['Personabeschrieb']) | |
| ] | |
| example_cases = [ | |
| list(x) for x in zip(personas, example_cases_df['Problembeschrieb']) | |
| ] | |
| available_therapies = '\n'.join(example_cases_df['Namen von Empfohlenen Therapien'].tolist()) | |
| available_therapies = sorted(list(set(x for x in available_therapies.split('\n') if x))) | |
| theme = gr.themes.Default() | |
| with gr.Blocks(theme=theme) as demo: | |
| with gr.Row(): | |
| with gr.Column(): | |
| persona = gr.Textbox(label="Persona") | |
| problem = gr.Textbox(label="Problem") | |
| gr.ClearButton(components=[persona, problem]) | |
| examples = gr.Examples( | |
| examples=example_cases, | |
| inputs=[persona, problem], | |
| ) | |
| model = gr.Radio( | |
| choices=['gpt-3.5-turbo', 'gpt-4o', 'gpt-4-turbo', 'gpt-4'], | |
| value='gpt-3.5-turbo', | |
| label='Model choice' | |
| ) | |
| therapies = gr.Textbox( | |
| label='Verfügbare Therapien (löschen um keine Auswahl vorzugeben)', | |
| value=', '.join(available_therapies), | |
| show_label=True | |
| ) | |
| with gr.Column(visible=True) as output_col: | |
| output = gr.Textbox(label="Empfohlene Therapien", placeholder="(Hier werden empfohlene Therapien angezeigt.)") | |
| go_btn = gr.Button("Therapien finden", variant='primary') | |
| go_btn.click(fn=get_recommendations, inputs=[persona, problem, model, therapies], outputs=output, api_name="recommend") | |
| demo.launch(auth=("well", "vida"), share=True) | |