Spaces:
Runtime error
Runtime error
Commit
·
0679fd5
1
Parent(s):
265dbd1
Update app.py
Browse files
app.py
CHANGED
@@ -2,42 +2,54 @@ import gradio as gr
|
|
2 |
import openai
|
3 |
|
4 |
DESCRIPTION = """
|
5 |
-
Hãy để trợ lý của AI Consultant hỗ trợ bạn
|
6 |
"""
|
7 |
|
8 |
# Define inputs and outputs
|
9 |
inputs = [
|
10 |
-
gr.inputs.Textbox(label="
|
11 |
]
|
12 |
|
13 |
outputs = [
|
14 |
gr.outputs.Textbox(label="Câu trả lời:")
|
15 |
]
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
openai.api_key = "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"
|
19 |
|
|
|
|
|
|
|
|
|
20 |
response = openai.Completion.create(
|
21 |
engine="text-davinci-003",
|
22 |
-
prompt=
|
23 |
-
max_tokens=
|
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()
|
|
|
2 |
import openai
|
3 |
|
4 |
DESCRIPTION = """
|
5 |
+
Hãy để trợ lý của AI Consultant hỗ trợ bạn!
|
6 |
"""
|
7 |
|
8 |
# Define inputs and outputs
|
9 |
inputs = [
|
10 |
+
gr.inputs.Textbox(label="Lịch sử trò chuyện:", type="text")
|
11 |
]
|
12 |
|
13 |
outputs = [
|
14 |
gr.outputs.Textbox(label="Câu trả lời:")
|
15 |
]
|
16 |
|
17 |
+
# Initialize conversation history
|
18 |
+
conversation_history = []
|
19 |
+
|
20 |
+
def chatbot(history):
|
21 |
+
global conversation_history
|
22 |
openai.api_key = "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"
|
23 |
|
24 |
+
# Use the provided history as conversation history
|
25 |
+
conversation_history = history.split('\n')
|
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=1000 # Adjust as needed
|
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()
|