Spaces:
Sleeping
Sleeping
- app.py +49 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
|
5 |
+
openai.api_key = os.getenv("OPENAI_API_KEY") # Set this in your HF Space secret settings
|
6 |
+
|
7 |
+
def chat_with_gpt4(user_question):
|
8 |
+
# Few-shot prompt context
|
9 |
+
prompt = f"""
|
10 |
+
You are a helpful customer support assistant.
|
11 |
+
|
12 |
+
Q: What is the return policy?
|
13 |
+
A: You can return items within 30 days if they are in original condition.
|
14 |
+
|
15 |
+
Q: How do I track my order?
|
16 |
+
A: You will receive a tracking number via email once your order is shipped.
|
17 |
+
|
18 |
+
Q: What payment methods do you accept?
|
19 |
+
A: We accept credit cards, PayPal, and bank transfers.
|
20 |
+
|
21 |
+
Q: {user_question}
|
22 |
+
A:
|
23 |
+
"""
|
24 |
+
|
25 |
+
try:
|
26 |
+
response = openai.ChatCompletion.create(
|
27 |
+
model="gpt-4",
|
28 |
+
messages=[
|
29 |
+
{"role": "system", "content": "You are a helpful customer support assistant."},
|
30 |
+
{"role": "user", "content": prompt}
|
31 |
+
],
|
32 |
+
temperature=0.3,
|
33 |
+
max_tokens=150,
|
34 |
+
)
|
35 |
+
return response['choices'][0]['message']['content'].strip()
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
return f"Error: {str(e)}"
|
39 |
+
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=chat_with_gpt4,
|
42 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask a customer service question..."),
|
43 |
+
outputs=gr.Textbox(label="GPT-4 Response"),
|
44 |
+
title="Few-Shot Customer Support Chatbot (GPT-4)",
|
45 |
+
description="This chatbot uses GPT-4 with few-shot prompting to answer customer support questions."
|
46 |
+
)
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
gradio
|