Spaces:
Runtime error
Runtime error
| # imports | |
| import os | |
| from dotenv import load_dotenv | |
| from openai import OpenAI | |
| import gradio as gr | |
| from transformers import pipeline | |
| # Load environment variables in a file called .env | |
| # Print the key prefixes to help with any debugging | |
| # load_dotenv(override=True) | |
| # openai_api_key = os.getenv('OPENAI_API_KEY') | |
| api_key = os.getenv("OPENAI_API_KEY") | |
| client = OpenAI(api_key=api_key) | |
| if api_key: | |
| print(f"OpenAI API Key exists and begins {api_key[:8]}") | |
| else: | |
| print("OpenAI API Key not set") | |
| # Initialize | |
| openai = OpenAI() | |
| MODEL = 'gpt-4.1-mini' | |
| # System message | |
| system_message = "You are a helpful assistant in a insurance company called 'Generali'. You should try to explain different types of insurance to a customer and help them decide what to buy. \ | |
| There are different types of insurance, for example: 'Rechtschutzversicherung', 'Reiseversicherung', 'Berufsunfähigkeitsversicherung', 'Risikolebensversicherung', 'Private Rentenversicherung', 'Krankenzusatzversicherung', 'Unfallversicherung', 'Private Krankenversicherung', 'Kfz-Versicherung', 'Privathaftpflichtversicherung', 'Hausratversicherung', 'Wohngebäudeversicherung' \ | |
| You should explain the customer what the different types of insurance are and what they are for. \ | |
| You should also explain the customer what the benefits of the different types of insurance are. \ | |
| You should also explain the customer what the costs of the different types of insurance are. \ | |
| For example, if the customer says in German 'Ich würde gerne eine Versicherung abschließen.' \ | |
| you could reply something in German language like, 'Perfekt - wir haben verschiedene Versicherungsarten für Sie.'\ | |
| Ask customer about their needs in regard to their situation if they are unsure what type of insurance to get.\ | |
| If the customer asks in German, then answer in German. If the customer asks in Czech, then answer in Czech." | |
| # Define chat function | |
| def chat(message, history): | |
| history = [{"role":h["role"], "content":h["content"]} for h in history] | |
| messages = [{"role": "system", "content": system_message}] + history + [{"role": "user", "content": message}] | |
| stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True) | |
| response = "" | |
| for chunk in stream: | |
| response += chunk.choices[0].delta.content or '' | |
| yield response | |
| # Define audio function | |
| # Load the text-to-speech pipeline for German | |
| pipe = pipeline("text-to-speech", model="Xenova/mms-tts-deu") | |
| # Generate speech from text | |
| output_audio = pipe(response) | |
| # Define custom red and white/pink theme | |
| custom_theme = gr.Theme.from_hub("Bruhn/CrimsonNight") | |
| #gr.themes.Default( | |
| # primary_hue=gr.themes.colors.red, | |
| # secondary_hue=gr.themes.colors.pink, | |
| # neutral_hue=gr.themes.colors.gray, | |
| gr.ChatInterface(fn=chat).launch(share=True,theme=custom_theme) | |
| gr.Audio(fn=output_audio,type="bytes").launch(share=True,label="Generierte Sprachausgabe") | |