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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import firebase_admin
|
| 3 |
+
from firebase_admin import credentials, db
|
| 4 |
+
import requests
|
| 5 |
+
import os
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 9 |
+
|
| 10 |
+
# ---------- 1. Firebase Realtime Database Initialization ----------
|
| 11 |
+
cred = credentials.Certificate("firebase-key.json")
|
| 12 |
+
database_url = "https://chat-bot-80c6e-default-rtdb.firebaseio.com/"
|
| 13 |
+
firebase_admin.initialize_app(cred, {'databaseURL': database_url})
|
| 14 |
+
ref = db.reference('/')
|
| 15 |
+
|
| 16 |
+
# ---------- 2. OpenRouter API Setup ----------
|
| 17 |
+
OPENROUTER_API_KEY = os.getenv("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")
|
| 18 |
+
if not OPENROUTER_API_KEY:
|
| 19 |
+
print("Warning: OPENROUTER_API_KEY not set. OpenRouter will be unavailable.")
|
| 20 |
+
MODEL = "google/gemini-2.0-flash-lite-preview-02-05" # free model
|
| 21 |
+
|
| 22 |
+
def call_openrouter(prompt):
|
| 23 |
+
"""OpenRouter API call. Returns response string or None if fails."""
|
| 24 |
+
if not OPENROUTER_API_KEY:
|
| 25 |
+
return None
|
| 26 |
+
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 27 |
+
headers = {
|
| 28 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 29 |
+
"Content-Type": "application/json"
|
| 30 |
+
}
|
| 31 |
+
payload = {
|
| 32 |
+
"model": MODEL,
|
| 33 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 34 |
+
"temperature": 0.7,
|
| 35 |
+
"max_tokens": 500
|
| 36 |
+
}
|
| 37 |
+
try:
|
| 38 |
+
response = requests.post(url, json=payload, headers=headers, timeout=10)
|
| 39 |
+
response.raise_for_status()
|
| 40 |
+
data = response.json()
|
| 41 |
+
return data["choices"][0]["message"]["content"]
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"OpenRouter error: {e}")
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
# ---------- 3. Local GPT-2 Setup (Fallback) ----------
|
| 47 |
+
print("Loading GPT-2 model (first time will download)...")
|
| 48 |
+
try:
|
| 49 |
+
gpt2_model = GPT2LMModel.from_pretrained("gpt2")
|
| 50 |
+
gpt2_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 51 |
+
# Set pad token to eos token
|
| 52 |
+
gpt2_tokenizer.pad_token = gpt2_tokenizer.eos_token
|
| 53 |
+
print("GPT-2 loaded successfully (fallback ready).")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Failed to load GPT-2: {e}")
|
| 56 |
+
gpt2_model = None
|
| 57 |
+
|
| 58 |
+
def call_gpt2(prompt):
|
| 59 |
+
"""Local GPT-2 generation. Returns response string."""
|
| 60 |
+
if gpt2_model is None:
|
| 61 |
+
return "Local GPT-2 model unavailable. Please check installation."
|
| 62 |
+
try:
|
| 63 |
+
inputs = gpt2_tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 64 |
+
# Generate
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
outputs = gpt2_model.generate(
|
| 67 |
+
inputs.input_ids,
|
| 68 |
+
max_new_tokens=150,
|
| 69 |
+
temperature=0.7,
|
| 70 |
+
do_sample=True,
|
| 71 |
+
pad_token_id=gpt2_tokenizer.eos_token_id
|
| 72 |
+
)
|
| 73 |
+
response = gpt2_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 74 |
+
# Remove the input prompt from response (optional)
|
| 75 |
+
if response.startswith(prompt):
|
| 76 |
+
response = response[len(prompt):].strip()
|
| 77 |
+
return response if response else "GPT-2 didn't generate anything."
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return f"GPT-2 error: {e}"
|
| 80 |
+
|
| 81 |
+
# ---------- 4. Combined AI Function (Primary OpenRouter, Fallback GPT-2) ----------
|
| 82 |
+
def get_ai_response(prompt):
|
| 83 |
+
"""Try OpenRouter first; if fails, use GPT-2."""
|
| 84 |
+
print(f"Trying OpenRouter for: {prompt[:50]}...")
|
| 85 |
+
response = call_openrouter(prompt)
|
| 86 |
+
if response is not None and response.strip():
|
| 87 |
+
print("OpenRouter succeeded.")
|
| 88 |
+
return response
|
| 89 |
+
else:
|
| 90 |
+
print("OpenRouter failed or returned empty. Falling back to GPT-2.")
|
| 91 |
+
return call_gpt2(prompt)
|
| 92 |
+
|
| 93 |
+
# ---------- 5. Firebase Helpers ----------
|
| 94 |
+
def save_message(session_id, role, content):
|
| 95 |
+
messages_ref = ref.child(f"chats/{session_id}/messages")
|
| 96 |
+
new_msg_ref = messages_ref.push()
|
| 97 |
+
new_msg_ref.set({
|
| 98 |
+
"role": role,
|
| 99 |
+
"content": content,
|
| 100 |
+
"timestamp": datetime.now().isoformat()
|
| 101 |
+
})
|
| 102 |
+
|
| 103 |
+
def get_last_n_messages(session_id, n=5):
|
| 104 |
+
messages_ref = ref.child(f"chats/{session_id}/messages")
|
| 105 |
+
try:
|
| 106 |
+
all_msgs = messages_ref.get()
|
| 107 |
+
if not all_msgs:
|
| 108 |
+
return []
|
| 109 |
+
msg_list = []
|
| 110 |
+
for key, val in all_msgs.items():
|
| 111 |
+
msg_list.append(val)
|
| 112 |
+
msg_list.sort(key=lambda x: x.get('timestamp', ''), reverse=True)
|
| 113 |
+
return msg_list[:n]
|
| 114 |
+
except Exception as e:
|
| 115 |
+
print("Firebase read error:", e)
|
| 116 |
+
return []
|
| 117 |
+
|
| 118 |
+
# ---------- 6. Intent Classification (Simple) ----------
|
| 119 |
+
def classify_intent(text):
|
| 120 |
+
text_lower = text.lower()
|
| 121 |
+
if any(w in text_lower for w in ["hi", "hello", "salam", "namaste", "hey"]):
|
| 122 |
+
return "greeting"
|
| 123 |
+
elif any(w in text_lower for w in ["price", "cost", "kitne", "rate", "dam"]):
|
| 124 |
+
return "pricing"
|
| 125 |
+
elif any(w in text_lower for w in ["sad", "problem", "issue", "complaint", "shikayat"]):
|
| 126 |
+
return "complaint"
|
| 127 |
+
return "general"
|
| 128 |
+
|
| 129 |
+
# ---------- 7. Main Chat Function ----------
|
| 130 |
+
def chat_with_ai(user_message, session_id="default_user"):
|
| 131 |
+
# Intent detection
|
| 132 |
+
intent = classify_intent(user_message)
|
| 133 |
+
|
| 134 |
+
# Customize prompt based on intent (for better responses)
|
| 135 |
+
if intent == "greeting":
|
| 136 |
+
prompt = f"User greeted: {user_message}. Respond warmly and friendly."
|
| 137 |
+
elif intent == "pricing":
|
| 138 |
+
prompt = f"User asked about price: {user_message}. Give helpful pricing information."
|
| 139 |
+
elif intent == "complaint":
|
| 140 |
+
prompt = f"User complained: {user_message}. Respond empathetically and offer solution."
|
| 141 |
+
else:
|
| 142 |
+
prompt = user_message
|
| 143 |
+
|
| 144 |
+
# Save user message
|
| 145 |
+
save_message(session_id, "user", user_message)
|
| 146 |
+
|
| 147 |
+
# Get AI response (with fallback)
|
| 148 |
+
bot_reply = get_ai_response(prompt)
|
| 149 |
+
|
| 150 |
+
# Save bot reply
|
| 151 |
+
save_message(session_id, "assistant", bot_reply)
|
| 152 |
+
|
| 153 |
+
# Optionally, add a note if fallback was used? Not needed, but can be added
|
| 154 |
+
return bot_reply
|
| 155 |
+
|
| 156 |
+
# ---------- 8. Gradio Interface ----------
|
| 157 |
+
demo = gr.Interface(
|
| 158 |
+
fn=chat_with_ai,
|
| 159 |
+
inputs=[
|
| 160 |
+
gr.Textbox(label="Aap ka message", lines=2, placeholder="Yahan likhein..."),
|
| 161 |
+
gr.Textbox(label="Session ID (optional)", value="default_user", visible=False)
|
| 162 |
+
],
|
| 163 |
+
outputs=gr.Textbox(label="Chatbot ka jawab", lines=4),
|
| 164 |
+
title="🤖 AI Chatbot: OpenRouter + GPT-2 Fallback | Firebase Realtime DB",
|
| 165 |
+
description="Pehle OpenRouter (free Gemini) try karega. Agar fail ho to local GPT-2 automatically use hoga (unlimited). Har message Firebase mein save hoga.",
|
| 166 |
+
examples=[["Assalam-o-Alaikum!"], ["Mujhe laptop ki price chahiye"], ["Mera order late hai"]]
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
demo.launch()
|