Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
openai.api_key = api_key
|
| 8 |
|
| 9 |
# Store conversation history
|
|
@@ -11,61 +17,54 @@ conversation_history = []
|
|
| 11 |
|
| 12 |
def chatbot(prompt):
|
| 13 |
global conversation_history
|
| 14 |
-
|
| 15 |
-
# Update conversation history with the new user prompt
|
| 16 |
conversation_history.append({"role": "user", "content": prompt})
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
ai_role = "Freya"
|
| 31 |
-
|
| 32 |
-
# Generate AI response with updated role and conversation history
|
| 33 |
-
response = openai.ChatCompletion.create(
|
| 34 |
-
model="gpt-3.5-turbo-16k",
|
| 35 |
-
messages=conversation_history,
|
| 36 |
-
max_tokens=350,
|
| 37 |
-
stop="<break character>"
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
# Add AI response to conversation history
|
| 41 |
-
ai_response = response.choices[0].message.content
|
| 42 |
-
conversation_history.append({"role": "assistant", "content": ai_response})
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
#Define your HTML content
|
| 47 |
html_content = """
|
| 48 |
-
<div style='
|
| 49 |
-
<
|
| 50 |
-
<
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
<p> After you have asked her five or so questions, type in '<break character>' to see an analysis of your interview style.</p>
|
| 56 |
-
</div>
|
| 57 |
</div>
|
| 58 |
"""
|
| 59 |
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
-
gr.HTML(html_content)
|
| 62 |
with gr.Row():
|
| 63 |
input_text = gr.Textbox(lines=2, placeholder="Enter your question or command here...")
|
| 64 |
submit_button = gr.Button("Submit")
|
| 65 |
output_text = gr.Textbox()
|
| 66 |
submit_button.click(chatbot, inputs=input_text, outputs=output_text)
|
| 67 |
|
| 68 |
-
demo.launch(
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
+
import logging
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Logging setup
|
| 7 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 8 |
+
|
| 9 |
+
# Initialize the OpenAI client with your API key
|
| 10 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
if not api_key:
|
| 12 |
+
raise ValueError("OPENAI_API_KEY environment variable not set.")
|
| 13 |
openai.api_key = api_key
|
| 14 |
|
| 15 |
# Store conversation history
|
|
|
|
| 17 |
|
| 18 |
def chatbot(prompt):
|
| 19 |
global conversation_history
|
|
|
|
|
|
|
| 20 |
conversation_history.append({"role": "user", "content": prompt})
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
# Logic for role switching
|
| 24 |
+
if "<break character>" in prompt:
|
| 25 |
+
# Role: Entrepreneurship Educator
|
| 26 |
+
entrepreneurship_educator_prompt = "You are now speaking to..."
|
| 27 |
+
modified_prompt = entrepreneurship_educator_prompt + prompt.replace("<break character>", "")
|
| 28 |
+
conversation_history.append({"role": "system", "content": modified_prompt})
|
| 29 |
+
else:
|
| 30 |
+
# Role: Roberta
|
| 31 |
+
Roberta_prompt = "You will take on the role of Roberta..."
|
| 32 |
+
modified_prompt = Roberta_prompt + prompt
|
| 33 |
+
conversation_history.append({"role": "system", "content": modified_prompt})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# Generate AI response
|
| 36 |
+
response = openai.ChatCompletion.create(
|
| 37 |
+
model="gpt-4o-mini",
|
| 38 |
+
messages=conversation_history,
|
| 39 |
+
max_tokens=350,
|
| 40 |
+
stop="<break character>",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
ai_response = response.choices[0].message.content
|
| 44 |
+
conversation_history.append({"role": "assistant", "content": ai_response})
|
| 45 |
+
return ai_response
|
| 46 |
+
except Exception as e:
|
| 47 |
+
logging.error("Error in chatbot: %s", str(e))
|
| 48 |
+
return "An error occurred. Please try again later."
|
| 49 |
|
| 50 |
#Define your HTML content
|
| 51 |
html_content = """
|
| 52 |
+
<div style='text-align: <center;'>
|
| 53 |
+
<h2>Welcome to the Chatbot</h2>
|
| 54 |
+
<p>You are an entrepreneur exploring opportunities to help parents of young children.</p>
|
| 55 |
+
<p>You are conducting an interview with Roberta, a 30-something friend-of-a-friend who has a 2-year old daughter, Laurie.</p>
|
| 56 |
+
<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>
|
| 57 |
+
<p> After you have asked her five or so questions, type in '<break character>' to see an analysis of your interview style.
|
| 58 |
+
<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;'>
|
|
|
|
|
|
|
| 59 |
</div>
|
| 60 |
"""
|
| 61 |
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
+
gr.HTML(html_content)
|
| 64 |
with gr.Row():
|
| 65 |
input_text = gr.Textbox(lines=2, placeholder="Enter your question or command here...")
|
| 66 |
submit_button = gr.Button("Submit")
|
| 67 |
output_text = gr.Textbox()
|
| 68 |
submit_button.click(chatbot, inputs=input_text, outputs=output_text)
|
| 69 |
|
| 70 |
+
demo.launch(debug=True, share=True)
|
|
|
|
|
|
|
|
|