Spaces:
Sleeping
Sleeping
Ganesh Chintalapati
commited on
Commit
·
231773b
1
Parent(s):
aa39f2a
login info
Browse files- api.py +14 -16
- app.py +164 -49
- chat_history.db +0 -0
- chatapp.db +0 -0
- core.py +20 -10
- database.py +3 -3
api.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import httpx
|
| 3 |
import json
|
| 4 |
-
import traceback
|
| 5 |
from typing import AsyncGenerator, List, Dict
|
| 6 |
from config import logger
|
| 7 |
|
|
@@ -15,9 +14,10 @@ async def ask_openai(query: str, history: List[Dict[str, str]]) -> AsyncGenerato
|
|
| 15 |
|
| 16 |
messages = []
|
| 17 |
for msg in history:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
messages.append({"role": "user", "content": query})
|
| 22 |
|
| 23 |
headers = {
|
|
@@ -59,7 +59,6 @@ async def ask_openai(query: str, history: List[Dict[str, str]]) -> AsyncGenerato
|
|
| 59 |
logger.error(f"OpenAI API error: {e}")
|
| 60 |
yield f"[OpenAI Error]: {e}"
|
| 61 |
|
| 62 |
-
|
| 63 |
# ===== Anthropic =====
|
| 64 |
async def ask_anthropic(query: str, history: List[Dict[str, str]]) -> AsyncGenerator[str, None]:
|
| 65 |
anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
|
|
@@ -70,9 +69,10 @@ async def ask_anthropic(query: str, history: List[Dict[str, str]]) -> AsyncGener
|
|
| 70 |
|
| 71 |
messages = []
|
| 72 |
for msg in history:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
messages.append({"role": "user", "content": query})
|
| 77 |
|
| 78 |
headers = {
|
|
@@ -115,7 +115,6 @@ async def ask_anthropic(query: str, history: List[Dict[str, str]]) -> AsyncGener
|
|
| 115 |
logger.error(f"Anthropic API error: {e}")
|
| 116 |
yield f"[Anthropic Error]: {e}"
|
| 117 |
|
| 118 |
-
|
| 119 |
# ===== Gemini =====
|
| 120 |
async def ask_gemini(query: str, history: List[Dict[str, str]]) -> AsyncGenerator[str, None]:
|
| 121 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
|
@@ -126,9 +125,10 @@ async def ask_gemini(query: str, history: List[Dict[str, str]]) -> AsyncGenerato
|
|
| 126 |
|
| 127 |
history_text = ""
|
| 128 |
for msg in history:
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
| 132 |
full_prompt = f"{history_text}User: {query}\n"
|
| 133 |
|
| 134 |
headers = {"Content-Type": "application/json"}
|
|
@@ -153,8 +153,6 @@ async def ask_gemini(query: str, history: List[Dict[str, str]]) -> AsyncGenerato
|
|
| 153 |
try:
|
| 154 |
json_data = json.loads(buffer.strip(", \n"))
|
| 155 |
buffer = ""
|
| 156 |
-
|
| 157 |
-
# handle both list and dict format
|
| 158 |
objects = json_data if isinstance(json_data, list) else [json_data]
|
| 159 |
for obj in objects:
|
| 160 |
candidates = obj.get("candidates", [])
|
|
@@ -165,7 +163,7 @@ async def ask_gemini(query: str, history: List[Dict[str, str]]) -> AsyncGenerato
|
|
| 165 |
if text:
|
| 166 |
yield text
|
| 167 |
except json.JSONDecodeError:
|
| 168 |
-
continue
|
| 169 |
except Exception as e:
|
| 170 |
logger.error(f"Gemini API error: {e}")
|
| 171 |
-
yield f"[Gemini Error]: {e}"
|
|
|
|
| 1 |
import os
|
| 2 |
import httpx
|
| 3 |
import json
|
|
|
|
| 4 |
from typing import AsyncGenerator, List, Dict
|
| 5 |
from config import logger
|
| 6 |
|
|
|
|
| 14 |
|
| 15 |
messages = []
|
| 16 |
for msg in history:
|
| 17 |
+
if msg.get("role") == "user":
|
| 18 |
+
messages.append({"role": "user", "content": msg["content"]})
|
| 19 |
+
elif msg.get("role") == "assistant":
|
| 20 |
+
messages.append({"role": "assistant", "content": msg["content"]})
|
| 21 |
messages.append({"role": "user", "content": query})
|
| 22 |
|
| 23 |
headers = {
|
|
|
|
| 59 |
logger.error(f"OpenAI API error: {e}")
|
| 60 |
yield f"[OpenAI Error]: {e}"
|
| 61 |
|
|
|
|
| 62 |
# ===== Anthropic =====
|
| 63 |
async def ask_anthropic(query: str, history: List[Dict[str, str]]) -> AsyncGenerator[str, None]:
|
| 64 |
anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
|
|
|
|
| 69 |
|
| 70 |
messages = []
|
| 71 |
for msg in history:
|
| 72 |
+
if msg.get("role") == "user":
|
| 73 |
+
messages.append({"role": "user", "content": msg["content"]})
|
| 74 |
+
elif msg.get("role") == "assistant":
|
| 75 |
+
messages.append({"role": "assistant", "content": msg["content"]})
|
| 76 |
messages.append({"role": "user", "content": query})
|
| 77 |
|
| 78 |
headers = {
|
|
|
|
| 115 |
logger.error(f"Anthropic API error: {e}")
|
| 116 |
yield f"[Anthropic Error]: {e}"
|
| 117 |
|
|
|
|
| 118 |
# ===== Gemini =====
|
| 119 |
async def ask_gemini(query: str, history: List[Dict[str, str]]) -> AsyncGenerator[str, None]:
|
| 120 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
|
|
|
| 125 |
|
| 126 |
history_text = ""
|
| 127 |
for msg in history:
|
| 128 |
+
if msg.get("role") == "user":
|
| 129 |
+
history_text += f"User: {msg['content']}\n"
|
| 130 |
+
elif msg.get("role") == "assistant":
|
| 131 |
+
history_text += f"Assistant: {msg['content']}\n"
|
| 132 |
full_prompt = f"{history_text}User: {query}\n"
|
| 133 |
|
| 134 |
headers = {"Content-Type": "application/json"}
|
|
|
|
| 153 |
try:
|
| 154 |
json_data = json.loads(buffer.strip(", \n"))
|
| 155 |
buffer = ""
|
|
|
|
|
|
|
| 156 |
objects = json_data if isinstance(json_data, list) else [json_data]
|
| 157 |
for obj in objects:
|
| 158 |
candidates = obj.get("candidates", [])
|
|
|
|
| 163 |
if text:
|
| 164 |
yield text
|
| 165 |
except json.JSONDecodeError:
|
| 166 |
+
continue
|
| 167 |
except Exception as e:
|
| 168 |
logger.error(f"Gemini API error: {e}")
|
| 169 |
+
yield f"[Gemini Error]: {e}"
|
app.py
CHANGED
|
@@ -1,54 +1,169 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
with gr.Blocks() as demo:
|
| 5 |
gr.Markdown("# Multi-Model Chat with Login")
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
submit_button.click(
|
| 44 |
-
submit_query,
|
| 45 |
-
[query, providers, chat_history, user_id],
|
| 46 |
-
[query, openai_chatbot, anthropic_chatbot, gemini_chatbot, chat_history]
|
| 47 |
-
)
|
| 48 |
-
clear_button.click(
|
| 49 |
-
clear_history,
|
| 50 |
-
[user_id],
|
| 51 |
-
[openai_chatbot, anthropic_chatbot, gemini_chatbot, chat_history]
|
| 52 |
-
)
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import sqlite3
|
| 3 |
+
import os
|
| 4 |
+
import asyncio
|
| 5 |
+
from core import submit_query
|
| 6 |
+
|
| 7 |
+
DB_PATH = "chat_history.db"
|
| 8 |
+
|
| 9 |
+
def init_db():
|
| 10 |
+
conn = sqlite3.connect(DB_PATH)
|
| 11 |
+
c = conn.cursor()
|
| 12 |
+
c.execute("""
|
| 13 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 14 |
+
username TEXT PRIMARY KEY,
|
| 15 |
+
password TEXT NOT NULL
|
| 16 |
+
)
|
| 17 |
+
""")
|
| 18 |
+
c.execute("""
|
| 19 |
+
CREATE TABLE IF NOT EXISTS history (
|
| 20 |
+
username TEXT,
|
| 21 |
+
providers TEXT,
|
| 22 |
+
message TEXT,
|
| 23 |
+
openai_response TEXT,
|
| 24 |
+
anthropic_response TEXT,
|
| 25 |
+
gemini_response TEXT
|
| 26 |
+
)
|
| 27 |
+
""")
|
| 28 |
+
conn.commit()
|
| 29 |
+
conn.close()
|
| 30 |
+
|
| 31 |
+
def register_user(username, password):
|
| 32 |
+
conn = sqlite3.connect(DB_PATH)
|
| 33 |
+
c = conn.cursor()
|
| 34 |
+
try:
|
| 35 |
+
c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
|
| 36 |
+
conn.commit()
|
| 37 |
+
return True
|
| 38 |
+
except sqlite3.IntegrityError:
|
| 39 |
+
return False
|
| 40 |
+
finally:
|
| 41 |
+
conn.close()
|
| 42 |
+
|
| 43 |
+
def login_user(username, password):
|
| 44 |
+
conn = sqlite3.connect(DB_PATH)
|
| 45 |
+
c = conn.cursor()
|
| 46 |
+
c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
|
| 47 |
+
result = c.fetchone()
|
| 48 |
+
conn.close()
|
| 49 |
+
return result is not None
|
| 50 |
+
|
| 51 |
+
def get_chat_history(username):
|
| 52 |
+
conn = sqlite3.connect(DB_PATH)
|
| 53 |
+
c = conn.cursor()
|
| 54 |
+
c.execute("SELECT message, openai_response, anthropic_response, gemini_response FROM history WHERE username=?", (username,))
|
| 55 |
+
rows = c.fetchall()
|
| 56 |
+
conn.close()
|
| 57 |
+
history = []
|
| 58 |
+
for m, o, a, g in rows:
|
| 59 |
+
history.append({"role": "user", "content": m})
|
| 60 |
+
responses = []
|
| 61 |
+
if o: responses.append(f"OpenAI: {o}")
|
| 62 |
+
if a: responses.append(f"Anthropic: {a}")
|
| 63 |
+
if g: responses.append(f"Gemini: {g}")
|
| 64 |
+
if responses:
|
| 65 |
+
history.append({"role": "assistant", "content": "\n".join(responses)})
|
| 66 |
+
return history
|
| 67 |
+
|
| 68 |
+
def save_chat_history(username, providers, message, openai_resp, anthropic_resp, gemini_resp):
|
| 69 |
+
conn = sqlite3.connect(DB_PATH)
|
| 70 |
+
c = conn.cursor()
|
| 71 |
+
c.execute(
|
| 72 |
+
"INSERT INTO history (username, providers, message, openai_response, anthropic_response, gemini_response) VALUES (?, ?, ?, ?, ?, ?)",
|
| 73 |
+
(username, ",".join(providers), message, openai_resp, anthropic_resp, gemini_resp)
|
| 74 |
+
)
|
| 75 |
+
conn.commit()
|
| 76 |
+
conn.close()
|
| 77 |
+
|
| 78 |
+
async def chatbot_fn(message, history, username, providers):
|
| 79 |
+
if not username:
|
| 80 |
+
return "", history or []
|
| 81 |
+
if not providers:
|
| 82 |
+
return "", (history or []) + [
|
| 83 |
+
{"role": "user", "content": message},
|
| 84 |
+
{"role": "assistant", "content": "Please select at least one provider."}
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
+
final_result = None
|
| 88 |
+
async for _, openai_msgs, anthropic_msgs, gemini_msgs, updated_history in submit_query(message, providers, history, username):
|
| 89 |
+
final_result = (openai_msgs, anthropic_msgs, gemini_msgs, updated_history)
|
| 90 |
+
|
| 91 |
+
if not final_result:
|
| 92 |
+
return "", (history or []) + [
|
| 93 |
+
{"role": "user", "content": message},
|
| 94 |
+
{"role": "assistant", "content": "Error: No response received."}
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
openai_resp, anthropic_resp, gemini_resp, updated_history = final_result
|
| 98 |
+
|
| 99 |
+
def get_last_response(msgs):
|
| 100 |
+
if isinstance(msgs, list) and msgs:
|
| 101 |
+
for m in reversed(msgs):
|
| 102 |
+
if isinstance(m, dict) and m.get("role") == "assistant" and m.get("content"):
|
| 103 |
+
return m.get("content", "")
|
| 104 |
+
return ""
|
| 105 |
+
|
| 106 |
+
openai_text = get_last_response(openai_resp)
|
| 107 |
+
anthropic_text = get_last_response(anthropic_resp)
|
| 108 |
+
gemini_text = get_last_response(gemini_resp)
|
| 109 |
+
save_chat_history(username, providers, message, openai_text, anthropic_text, gemini_text)
|
| 110 |
+
|
| 111 |
+
responses = []
|
| 112 |
+
if openai_text: responses.append(f"OpenAI: {openai_text}")
|
| 113 |
+
if anthropic_text: responses.append(f"Anthropic: {anthropic_text}")
|
| 114 |
+
if gemini_text: responses.append(f"Gemini: {gemini_text}")
|
| 115 |
+
|
| 116 |
+
history = history or []
|
| 117 |
+
history.append({"role": "user", "content": message})
|
| 118 |
+
if responses:
|
| 119 |
+
history.append({"role": "assistant", "content": "\n".join(responses)})
|
| 120 |
+
return "", history
|
| 121 |
+
|
| 122 |
+
def load_history(username):
|
| 123 |
+
if not username:
|
| 124 |
+
return "", []
|
| 125 |
+
return "", get_chat_history(username)
|
| 126 |
|
| 127 |
with gr.Blocks() as demo:
|
| 128 |
gr.Markdown("# Multi-Model Chat with Login")
|
| 129 |
+
with gr.Tabs():
|
| 130 |
+
with gr.Tab("Login / Register"):
|
| 131 |
+
username = gr.Textbox(label="Username")
|
| 132 |
+
password = gr.Textbox(label="Password", type="password")
|
| 133 |
+
login_btn = gr.Button("Login")
|
| 134 |
+
register_btn = gr.Button("Register")
|
| 135 |
+
login_status = gr.Markdown("")
|
| 136 |
+
|
| 137 |
+
with gr.Tab("Chat"):
|
| 138 |
+
providers = gr.CheckboxGroup(
|
| 139 |
+
choices=["OpenAI", "Anthropic", "Gemini"],
|
| 140 |
+
label="Select Providers",
|
| 141 |
+
value=["OpenAI"]
|
| 142 |
+
)
|
| 143 |
+
chatbox = gr.Chatbot(type="messages")
|
| 144 |
+
msg = gr.Textbox(label="Message")
|
| 145 |
+
send_btn = gr.Button("Send")
|
| 146 |
+
clear_btn = gr.Button("Clear History")
|
| 147 |
+
hidden_username = gr.State("")
|
| 148 |
+
|
| 149 |
+
def do_login(u, p):
|
| 150 |
+
if login_user(u, p):
|
| 151 |
+
return f"Welcome, {u}!", gr.update(visible=True), u
|
| 152 |
+
else:
|
| 153 |
+
return "Login failed.", gr.update(visible=False), ""
|
| 154 |
+
|
| 155 |
+
def do_register(u, p):
|
| 156 |
+
if register_user(u, p):
|
| 157 |
+
return "Registration successful! Please login.", gr.update(visible=False), ""
|
| 158 |
+
else:
|
| 159 |
+
return "Username already exists.", gr.update(visible=False), ""
|
| 160 |
+
|
| 161 |
+
login_btn.click(do_login, [username, password], [login_status, chatbox, hidden_username])
|
| 162 |
+
register_btn.click(do_register, [username, password], [login_status, chatbox, hidden_username])
|
| 163 |
+
send_btn.click(chatbot_fn, [msg, chatbox, hidden_username, providers], [msg, chatbox])
|
| 164 |
+
clear_btn.click(lambda u: ("", []), [hidden_username], [msg, chatbox])
|
| 165 |
+
login_btn.click(load_history, [username], [msg, chatbox])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
+
init_db()
|
| 169 |
+
demo.launch()
|
chat_history.db
ADDED
|
Binary file (36.9 kB). View file
|
|
|
chatapp.db
CHANGED
|
Binary files a/chatapp.db and b/chatapp.db differ
|
|
|
core.py
CHANGED
|
@@ -17,7 +17,6 @@ def register(username, password, message=None):
|
|
| 17 |
def login(username, password, message=None):
|
| 18 |
user_id = db.get_user(username, password)
|
| 19 |
if user_id:
|
| 20 |
-
# Load last conversation if exists
|
| 21 |
conversation = db.get_conversations(user_id)
|
| 22 |
history = json.loads(conversation) if conversation else []
|
| 23 |
return "Login successful", user_id, history
|
|
@@ -33,7 +32,6 @@ def clear_history(user_id):
|
|
| 33 |
|
| 34 |
async def submit_query(query, providers, history, user_id):
|
| 35 |
async for _, openai_msgs, anthropic_msgs, gemini_msgs, updated_history in query_model(query, providers, history):
|
| 36 |
-
# Save updated history for the user
|
| 37 |
db.add_conversation(user_id, json.dumps(updated_history))
|
| 38 |
yield "", openai_msgs, anthropic_msgs, gemini_msgs, updated_history
|
| 39 |
|
|
@@ -43,18 +41,30 @@ async def query_model(query: str, providers: List[str], history: List[Dict[str,
|
|
| 43 |
anthropic_msgs = history.copy()
|
| 44 |
gemini_msgs = history.copy()
|
| 45 |
|
|
|
|
| 46 |
if "OpenAI" in providers:
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if "Anthropic" in providers:
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
if "Gemini" in providers:
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
updated_history = history
|
|
|
|
| 57 |
yield "", openai_msgs, anthropic_msgs, gemini_msgs, updated_history
|
| 58 |
except Exception as e:
|
| 59 |
logger.error(f"Error in query_model: {e}")
|
| 60 |
-
yield f"Error: An unexpected error occurred. {e}", [], [], [], history
|
|
|
|
| 17 |
def login(username, password, message=None):
|
| 18 |
user_id = db.get_user(username, password)
|
| 19 |
if user_id:
|
|
|
|
| 20 |
conversation = db.get_conversations(user_id)
|
| 21 |
history = json.loads(conversation) if conversation else []
|
| 22 |
return "Login successful", user_id, history
|
|
|
|
| 32 |
|
| 33 |
async def submit_query(query, providers, history, user_id):
|
| 34 |
async for _, openai_msgs, anthropic_msgs, gemini_msgs, updated_history in query_model(query, providers, history):
|
|
|
|
| 35 |
db.add_conversation(user_id, json.dumps(updated_history))
|
| 36 |
yield "", openai_msgs, anthropic_msgs, gemini_msgs, updated_history
|
| 37 |
|
|
|
|
| 41 |
anthropic_msgs = history.copy()
|
| 42 |
gemini_msgs = history.copy()
|
| 43 |
|
| 44 |
+
openai_response = ""
|
| 45 |
if "OpenAI" in providers:
|
| 46 |
+
async for chunk in ask_openai(query, history):
|
| 47 |
+
openai_response += chunk
|
| 48 |
+
if openai_response:
|
| 49 |
+
openai_msgs.append({"role": "assistant", "content": openai_response.strip()})
|
| 50 |
+
|
| 51 |
+
anthropic_response = ""
|
| 52 |
if "Anthropic" in providers:
|
| 53 |
+
async for chunk in ask_anthropic(query, history):
|
| 54 |
+
anthropic_response += chunk
|
| 55 |
+
if anthropic_response:
|
| 56 |
+
anthropic_msgs.append({"role": "assistant", "content": anthropic_response.strip()})
|
| 57 |
+
|
| 58 |
+
gemini_response = ""
|
| 59 |
if "Gemini" in providers:
|
| 60 |
+
async for chunk in ask_gemini(query, history):
|
| 61 |
+
gemini_response += chunk
|
| 62 |
+
if gemini_response:
|
| 63 |
+
gemini_msgs.append({"role": "assistant", "content": gemini_response.strip()})
|
| 64 |
|
| 65 |
+
updated_history = history.copy()
|
| 66 |
+
updated_history.append({"role": "user", "content": query})
|
| 67 |
yield "", openai_msgs, anthropic_msgs, gemini_msgs, updated_history
|
| 68 |
except Exception as e:
|
| 69 |
logger.error(f"Error in query_model: {e}")
|
| 70 |
+
yield f"Error: An unexpected error occurred. {e}", [], [], [], history
|
database.py
CHANGED
|
@@ -2,7 +2,7 @@ import sqlite3
|
|
| 2 |
from typing import Optional
|
| 3 |
|
| 4 |
class Database:
|
| 5 |
-
def __init__(self, db_path="
|
| 6 |
self.db_path = db_path
|
| 7 |
self.conn = None
|
| 8 |
|
|
@@ -28,7 +28,7 @@ class Database:
|
|
| 28 |
def add_user(self, username, password):
|
| 29 |
c = self.conn.cursor()
|
| 30 |
try:
|
| 31 |
-
c.execute("INSERT INTO users (username, password) VALUES (
|
| 32 |
self.conn.commit()
|
| 33 |
return True
|
| 34 |
except sqlite3.IntegrityError:
|
|
@@ -54,4 +54,4 @@ class Database:
|
|
| 54 |
def clear_conversation(self, user_id):
|
| 55 |
c = self.conn.cursor()
|
| 56 |
c.execute("DELETE FROM conversations WHERE user_id=?", (user_id,))
|
| 57 |
-
self.conn.commit()
|
|
|
|
| 2 |
from typing import Optional
|
| 3 |
|
| 4 |
class Database:
|
| 5 |
+
def __init__(self, db_path="chat_history.db"):
|
| 6 |
self.db_path = db_path
|
| 7 |
self.conn = None
|
| 8 |
|
|
|
|
| 28 |
def add_user(self, username, password):
|
| 29 |
c = self.conn.cursor()
|
| 30 |
try:
|
| 31 |
+
c.execute("INSERT INTO users (username, password) VALUES (_, _)", (username, password))
|
| 32 |
self.conn.commit()
|
| 33 |
return True
|
| 34 |
except sqlite3.IntegrityError:
|
|
|
|
| 54 |
def clear_conversation(self, user_id):
|
| 55 |
c = self.conn.cursor()
|
| 56 |
c.execute("DELETE FROM conversations WHERE user_id=?", (user_id,))
|
| 57 |
+
self.conn.commit()
|