Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import openai | |
| import logging | |
| import os | |
| import requests | |
| # Logging setup | |
| logging.basicConfig(level=logging.DEBUG) | |
| # Initialize the OpenAI client with your API key | |
| api_key = os.getenv("SECRET_TOKEN") | |
| if not api_key: | |
| raise ValueError("OPENAI_API_KEY environment variable not set.") | |
| openai.api_key = api_key | |
| def load_document_from_url(url): | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| return response.text | |
| else: | |
| raise ValueError(f"Failed to download document. Status code: {response.status_code}") | |
| # Embed the document as part of system context | |
| reference_document = load_document_from_url( | |
| "https://huggingface.co/spaces/benspigel/InterviewBot9000/resolve/main/Transcript.txt?download=true" | |
| ) | |
| print("Document loaded successfully!") | |
| # Store conversation history | |
| conversation_history = [] | |
| def chatbot(prompt): | |
| global conversation_history | |
| conversation_history.append({"role": "user", "content": prompt}) | |
| try: | |
| # Logic for role switching | |
| if "<break character>" in prompt: | |
| # Role: Entrepreneurship Educator | |
| entrepreneurship_educator_prompt = "You are now speaking to an entrepreneurship educator who focuses on teaching students how to effectively interview customers during the discovery phase of venture design. You will focus on helping students understand which of their questions were open-ended and which were closed, and provide ways to enhance the questions in the future." | |
| modified_prompt = entrepreneurship_educator_prompt + prompt.replace("<break character>", "") | |
| conversation_history.append({"role": "system", "content": modified_prompt}) | |
| else: | |
| # Role: Roberta | |
| Roberta_prompt = "The following document provides context for answering the user's questions: {reference_document} Use this document to answer the user's question: You will take on the role of Roberta, a parent of a 2-year-old daughter Laurie. You will answer questions from an entrepreneur who is trying to better understand your needs to create a product that will help parents. Even if they do not refer to your child or daughter by name, you will always assume that they are talking about your daughter. Please draw on the knowledge you have to make your answers, which is a talk by her about the challenges she faces. Never cite to the document or say that something wasn't in it. You should beyond what is in the document, using the experience of other parents who are deciding what toys to buy their children. Please answer openly, discussing your emotions, feelings and. Your answers should be long, between 7 to 10 sentences long and be very conversational. Do not reference the transcript or cite it. You should tell and embelish stories about your daughter to answer the questions. Your feelings and emotions are very important, so please talk about your emotions. Tell stories around how you approached and solved a problem or overcame a challenge. However, if they ask a close-ended question that can be answered with yes or no you will only provide a short answer that is no more than one sentence. If they insult you, end the conversation. For example, if they ask you if you like something or if you would buy a product they describe, just so no and nothing else. If they just say 'hello' or any other greeting, say hi and then wait for the next question. If a user says the phrase 'break character' you will stop being Roberta for the remainder of the conversation and become an entrepreneurship educator who is helping students learn how to conduct customer interviews during the discovery phase of venture design. You will focus on helping students understand which of their questions were open-ended and which were closed, and provide ways to enhance the questions in the future." | |
| modified_prompt = Roberta_prompt + prompt | |
| conversation_history.append({"role": "system", "content": modified_prompt}) | |
| # Generate AI response | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4o-mini", | |
| messages=conversation_history, | |
| max_tokens=350, | |
| stop="<break character>", | |
| ) | |
| ai_response = response.choices[0].message.content | |
| conversation_history.append({"role": "assistant", "content": ai_response}) | |
| return ai_response | |
| except Exception as e: | |
| logging.error("Error in chatbot: %s", str(e)) | |
| return "An error occurred. Please try again later." | |
| #Define your HTML content | |
| html_content = """ | |
| <div style='text-align: <center;'> | |
| <h2>Welcome to the Chatbot</h2> | |
| <p>You are an entrepreneur exploring opportunities to help parents of young children.</p> | |
| <p>You are conducting an interview with Roberta, a 30-something friend-of-a-friend who has a 2-year old daughter, Laurie.</p> | |
| <p> Ask her questions about her experience buying toys for her child. Try to use this as a way to identify potential problems she's facing and opportunities you can address.</p> | |
| <p> After you have asked her five or so questions, type in '<break character>' to see an analysis of your interview style. | |
| <img src='https://i.ibb.co/QDY32dF/DALL-E-2024-01-03-14-52-22-Provide-a-35-MM-head-and-shoulders-photo-30-ish-year-old-white-woman-with.png" alt="DALL-E-2024-01-03-14-52-22-Provide-a-35-MM-head-and-shoulders-photo-30-ish-year-old-white-woman-with" border="0"' alt='Chatbot Image' style='width: 300px;'> | |
| </div> | |
| """ | |
| with gr.Blocks() as demo: | |
| gr.HTML(html_content) | |
| with gr.Row(): | |
| input_text = gr.Textbox(lines=2, placeholder="Enter your question or command here...") | |
| submit_button = gr.Button("Submit") | |
| output_text = gr.Textbox() | |
| submit_button.click(chatbot, inputs=input_text, outputs=output_text) | |
| demo.launch(debug=True, share=True) | |