import openai import gradio as gr import os openai.api_key = os.getenv("OPENAI_API_KEY") # Set this in your HF Space secret settings def chat_with_gpt4(user_question): # Few-shot prompt context prompt = f""" You are a helpful customer support assistant. Q: What is the return policy? A: You can return items within 30 days if they are in original condition. Q: How do I track my order? A: You will receive a tracking number via email once your order is shipped. Q: What payment methods do you accept? A: We accept credit cards, PayPal, and bank transfers. Q: {user_question} A: """ try: response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a helpful customer support assistant."}, {"role": "user", "content": prompt} ], temperature=0.3, max_tokens=150, ) return response['choices'][0]['message']['content'].strip() except Exception as e: return f"Error: {str(e)}" iface = gr.Interface( fn=chat_with_gpt4, inputs=gr.Textbox(lines=2, placeholder="Ask a customer service question..."), outputs=gr.Textbox(label="GPT-4 Response"), title="Few-Shot Customer Support Chatbot (GPT-4)", description="This chatbot uses GPT-4 with few-shot prompting to answer customer support questions." ) if __name__ == "__main__": iface.launch()