| import gradio as gr |
| import openai |
| import base64 |
| import os |
| import sqlite3 |
| from datetime import datetime |
| from dotenv import load_dotenv |
|
|
| load_dotenv() |
|
|
| client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY")) |
|
|
| def init_db(): |
| conn = sqlite3.connect("complaints.db") |
| c = conn.cursor() |
| c.execute(""" |
| CREATE TABLE IF NOT EXISTS complaints ( |
| id INTEGER PRIMARY KEY AUTOINCREMENT, |
| complaint_text TEXT, |
| image_path TEXT, |
| ai_response TEXT, |
| created_at TEXT, |
| status TEXT DEFAULT 'resolved' |
| ) |
| """) |
| conn.commit() |
| conn.close() |
|
|
| def save_complaint(complaint_text, ai_response): |
| conn = sqlite3.connect("complaints.db") |
| c = conn.cursor() |
| c.execute(""" |
| INSERT INTO complaints |
| (complaint_text, image_path, ai_response, created_at, status) |
| VALUES (?, ?, ?, ?, ?) |
| """, (complaint_text, "uploaded", ai_response, |
| str(datetime.now()), "resolved")) |
| conn.commit() |
| conn.close() |
|
|
| def encode_image(image_path): |
| with open(image_path, "rb") as f: |
| return base64.b64encode(f.read()).decode("utf-8") |
|
|
| def analyze_complaint(image, complaint_text): |
| if image is None: |
| return "Please upload a product image." |
| if not complaint_text: |
| return "Please describe your complaint." |
| try: |
| base64_image = encode_image(image) |
| response = client.chat.completions.create( |
| model="gpt-4o", |
| messages=[ |
| { |
| "role": "system", |
| "content": """You are a helpful customer support agent. |
| Analyze the product image and complaint together. |
| Provide: |
| 1. Acknowledgment of the issue |
| 2. What you can see in the image |
| 3. Recommended solution |
| 4. Next steps for the customer""" |
| }, |
| { |
| "role": "user", |
| "content": [ |
| { |
| "type": "text", |
| "text": f"Customer complaint: {complaint_text}" |
| }, |
| { |
| "type": "image_url", |
| "image_url": { |
| "url": f"data:image/jpeg;base64,{base64_image}" |
| } |
| } |
| ] |
| } |
| ], |
| max_tokens=500 |
| ) |
| ai_response = response.choices[0].message.content |
| save_complaint(complaint_text, ai_response) |
| return f"π€ AI Response\n\n{ai_response}" |
| except Exception as e: |
| return f"Error: {str(e)}" |
|
|
| def get_history(): |
| try: |
| conn = sqlite3.connect("complaints.db") |
| c = conn.cursor() |
| c.execute(""" |
| SELECT id, complaint_text, ai_response, |
| created_at, status |
| FROM complaints |
| ORDER BY id DESC LIMIT 5 |
| """) |
| rows = c.fetchall() |
| conn.close() |
| if not rows: |
| return "No complaints yet." |
| history = f"Total recent complaints: {len(rows)}\n\n" |
| for row in rows: |
| history += f"ID: {row[0]}\n" |
| history += f"Complaint: {row[1]}\n" |
| history += f"Status: {row[4]}\n" |
| history += f"Time: {row[3]}\n" |
| history += "-" * 40 + "\n" |
| return history |
| except Exception as e: |
| return f"Error: {str(e)}" |
|
|
| init_db() |
|
|
| with gr.Blocks( |
| title="AI Customer Support Agent", |
| theme=gr.themes.Soft() |
| ) as demo: |
|
|
| gr.Markdown(""" |
| # π€ AI Customer Support Agent |
| ### Upload a product image and describe your complaint |
| *Powered by GPT-4V β Multimodal AI* |
| """) |
|
|
| with gr.Row(): |
| with gr.Column(): |
| image_input = gr.Image( |
| type="filepath", |
| label="πΈ Upload Product Image" |
| ) |
| complaint_input = gr.Textbox( |
| label="π Describe Your Complaint", |
| placeholder="Example: I received a damaged product.", |
| lines=4 |
| ) |
| submit_btn = gr.Button( |
| "π Analyze Complaint", |
| variant="primary" |
| ) |
| with gr.Column(): |
| response_output = gr.Textbox( |
| label="π€ AI Response", |
| lines=12, |
| interactive=False |
| ) |
|
|
| gr.Markdown("---") |
|
|
| with gr.Row(): |
| history_btn = gr.Button("π View Complaint History") |
| history_output = gr.Textbox( |
| label="Complaint History", |
| lines=8, |
| interactive=False |
| ) |
|
|
| submit_btn.click( |
| fn=analyze_complaint, |
| inputs=[image_input, complaint_input], |
| outputs=response_output |
| ) |
|
|
| history_btn.click( |
| fn=get_history, |
| inputs=[], |
| outputs=history_output |
| ) |
|
|
| gr.Markdown(""" |
| ### How it works: |
| 1. Upload a photo of your damaged/wrong product |
| 2. Describe your complaint in text |
| 3. AI analyzes BOTH image and text together |
| 4. Get an instant resolution response |
| """) |
|
|
| if __name__ == "__main__": |
| demo.launch() |