Spaces:
Runtime error
Runtime error
Commit ·
e973c3a
1
Parent(s): 11c687a
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,30 +14,42 @@ outputs = [
|
|
| 14 |
gr.outputs.Textbox(label="Câu trả lời:")
|
| 15 |
]
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
def chatbot(input):
|
|
|
|
| 18 |
openai.api_key = "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"
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
response = openai.Completion.create(
|
| 21 |
engine="text-davinci-003",
|
| 22 |
-
prompt=
|
| 23 |
max_tokens=60
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
|
|
|
| 30 |
interface = gr.Interface(
|
| 31 |
fn=chatbot,
|
| 32 |
inputs=inputs,
|
| 33 |
outputs=outputs,
|
| 34 |
-
title="AI Consultant",
|
| 35 |
-
theme="compact",
|
| 36 |
-
layout="vertical",
|
| 37 |
allow_flagging="never",
|
| 38 |
-
live=False,
|
| 39 |
-
description=DESCRIPTION,
|
| 40 |
css='style.css'
|
| 41 |
)
|
|
|
|
| 42 |
# Launch the interface
|
| 43 |
-
interface.launch()
|
|
|
|
| 14 |
gr.outputs.Textbox(label="Câu trả lời:")
|
| 15 |
]
|
| 16 |
|
| 17 |
+
# Initialize conversation history
|
| 18 |
+
conversation_history = []
|
| 19 |
+
|
| 20 |
def chatbot(input):
|
| 21 |
+
global conversation_history
|
| 22 |
openai.api_key = "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"
|
| 23 |
|
| 24 |
+
# Add user input to conversation history
|
| 25 |
+
conversation_history.append(f"You: {input}")
|
| 26 |
+
|
| 27 |
+
# Generate response using conversation history
|
| 28 |
response = openai.Completion.create(
|
| 29 |
engine="text-davinci-003",
|
| 30 |
+
prompt='\n'.join(conversation_history),
|
| 31 |
max_tokens=60
|
| 32 |
)
|
| 33 |
|
| 34 |
+
# Add AI response to conversation history
|
| 35 |
+
ai_response = response.choices[0].text.strip()
|
| 36 |
+
conversation_history.append(f"AI: {ai_response}")
|
| 37 |
+
|
| 38 |
+
return ai_response
|
| 39 |
|
| 40 |
+
# Create a Gradio interface
|
| 41 |
interface = gr.Interface(
|
| 42 |
fn=chatbot,
|
| 43 |
inputs=inputs,
|
| 44 |
outputs=outputs,
|
| 45 |
+
title="AI Consultant",
|
| 46 |
+
theme="compact",
|
| 47 |
+
layout="vertical",
|
| 48 |
allow_flagging="never",
|
| 49 |
+
live=False,
|
| 50 |
+
description=DESCRIPTION,
|
| 51 |
css='style.css'
|
| 52 |
)
|
| 53 |
+
|
| 54 |
# Launch the interface
|
| 55 |
+
interface.launch()
|