Spaces:
Sleeping
Sleeping
Commit ·
a8612d1
1
Parent(s): b585678
addded poc code
Browse files
app.py
CHANGED
|
@@ -1,64 +1,134 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 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 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import chromadb
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
# Initialize ChromaDB
|
| 7 |
+
chroma_client = chromadb.Client()
|
| 8 |
+
collection = chroma_client.get_or_create_collection("chat_memory")
|
| 9 |
+
|
| 10 |
+
# Load initial messages
|
| 11 |
+
trip_messages = [
|
| 12 |
+
{"id": "chat_001", "user": "Nithin", "text": "Hey Nithish, are we still on for the Goa trip?", "timestamp": "2024-04-20T09:00"},
|
| 13 |
+
{"id": "chat_002", "user": "Nithish", "text": "Yes bro, I was actually looking at places to stay near Baga beach.", "timestamp": "2024-04-20T09:05"},
|
| 14 |
+
{"id": "chat_003", "user": "Nithin", "text": "Awesome! Let’s book a beach shack or a villa.", "timestamp": "2024-04-20T09:10"},
|
| 15 |
+
{"id": "chat_004", "user": "Nithish", "text": "Villa sounds better. Should I check Airbnb or OYO?", "timestamp": "2024-04-20T09:15"},
|
| 16 |
+
{"id": "chat_005", "user": "Nithin", "text": "Airbnb. I’ve seen some great ones with pools and ocean views.", "timestamp": "2024-04-20T09:20"},
|
| 17 |
+
{"id": "chat_006", "user": "Nithish", "text": "Cool, let’s finalize the travel dates. Are you free on May 10th?", "timestamp": "2024-04-20T09:25"},
|
| 18 |
+
{"id": "chat_007", "user": "Nithin", "text": "Yes! Let’s plan from May 10 to May 14. 4 nights should be enough.", "timestamp": "2024-04-20T09:30"},
|
| 19 |
+
{"id": "chat_008", "user": "Nithish", "text": "Perfect. Let’s book the flights today and split the cost.", "timestamp": "2024-04-20T09:35"},
|
| 20 |
+
{"id": "chat_009", "user": "Nithin", "text": "Should we rent scooters there? It’ll be easier to move around.", "timestamp": "2024-04-20T09:40"},
|
| 21 |
+
{"id": "chat_010", "user": "Nithish", "text": "Yes! Let’s also hit up Thalassa and Curlies for sure!", "timestamp": "2024-04-20T09:45"},
|
| 22 |
+
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
# Add messages to ChromaDB
|
| 26 |
+
collection.add(
|
| 27 |
+
documents=[msg["text"] for msg in trip_messages],
|
| 28 |
+
metadatas=[{"user": msg["user"], "timestamp": msg["timestamp"]} for msg in trip_messages],
|
| 29 |
+
ids=[msg["id"] for msg in trip_messages]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Preload messages into chat history
|
| 33 |
+
preloaded_history = [(msg["text"], None) if msg["user"] == "Nithin" else (None, msg["text"]) for msg in trip_messages]
|
| 34 |
+
|
| 35 |
+
# OpenAI API Key
|
| 36 |
+
api_key = "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" # Replace with your OpenAI API key
|
| 37 |
+
message_counter = len(trip_messages) + 1
|
| 38 |
+
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("## ✈️ Goa Trip Plan Chat")
|
| 41 |
+
|
| 42 |
+
chatbot = gr.Chatbot(value=preloaded_history, label="Trip Chat")
|
| 43 |
+
|
| 44 |
+
chroma_suggestions = gr.Markdown("**🔍 Similar Past Conversations will appear here...**")
|
| 45 |
+
llm_suggestion = gr.Markdown("**🤖 Smart LLM Suggestion will appear here...**")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
msg1 = gr.Textbox(label="Nithin", placeholder="Type as Nithin...")
|
| 49 |
+
send1 = gr.Button("Send as Nithin")
|
| 50 |
+
|
| 51 |
+
msg2 = gr.Textbox(label="Nithish", placeholder="Type as Nithish...")
|
| 52 |
+
send2 = gr.Button("Send as Nithish")
|
| 53 |
+
|
| 54 |
+
state = gr.State(preloaded_history)
|
| 55 |
+
counter = gr.State(message_counter)
|
| 56 |
+
|
| 57 |
+
def send_message(user, message, history, count):
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Query similar messages from Chroma
|
| 61 |
+
try:
|
| 62 |
+
results = collection.query(query_texts=[message], n_results=3)
|
| 63 |
+
matches = results["documents"][0]
|
| 64 |
+
chroma_md = "🔍 **Similar Conversations:**\n" + "\n".join([f"- {m}" for m in matches])
|
| 65 |
+
except Exception as e:
|
| 66 |
+
chroma_md = f"_ChromaDB error: {e}_"
|
| 67 |
+
matches = []
|
| 68 |
+
|
| 69 |
+
# Generate LLM response using matched context
|
| 70 |
+
try:
|
| 71 |
+
prompt = f"""You are replying as **{user}** in a group chat planning a trip.
|
| 72 |
+
|
| 73 |
+
Here are some relevant past messages from the chat:
|
| 74 |
+
|
| 75 |
+
{"".join([f"- [{metadata['timestamp']}] {metadata['user']}: {text}\n" for text, metadata in zip(matches, results['metadatas'][0])])}
|
| 76 |
+
|
| 77 |
+
Now, as {user}, respond naturally to the following new message:
|
| 78 |
+
|
| 79 |
+
\"{message}\"
|
| 80 |
+
|
| 81 |
+
Make sure your tone matches a casual, friendly group chat.
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
response = requests.post(
|
| 85 |
+
url="https://api.openai.com/v1/chat/completions",
|
| 86 |
+
headers={
|
| 87 |
+
"Authorization": f"Bearer {api_key}",
|
| 88 |
+
"Content-Type": "application/json"
|
| 89 |
+
},
|
| 90 |
+
json={
|
| 91 |
+
"model": "gpt-4o",
|
| 92 |
+
"messages": [
|
| 93 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 94 |
+
{"role": "user", "content": prompt}
|
| 95 |
+
]
|
| 96 |
+
}
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
if response.status_code == 200:
|
| 100 |
+
llm_reply = response.json()["choices"][0]["message"]["content"]
|
| 101 |
+
else:
|
| 102 |
+
llm_reply = "_LLM failed to respond._"
|
| 103 |
+
except Exception as e:
|
| 104 |
+
llm_reply = f"_LLM error: {e}_"
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
# Add new message to history
|
| 108 |
+
timestamp = datetime.now().isoformat(timespec="minutes")
|
| 109 |
+
msg_id = f"chat_{count:03d}"
|
| 110 |
+
|
| 111 |
+
collection.add(
|
| 112 |
+
documents=[message],
|
| 113 |
+
metadatas=[{"user": user, "timestamp": timestamp}],
|
| 114 |
+
ids=[msg_id]
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
history.append((message, None) if user == "Nithin" else (None, message))
|
| 118 |
+
|
| 119 |
+
return history, "", history, count + 1, chroma_md, f"🤖 **LLM Suggestion:**\n\n{llm_reply}"
|
| 120 |
+
|
| 121 |
+
# Hook buttons
|
| 122 |
+
send1.click(
|
| 123 |
+
fn=lambda msg, hist, count: send_message("Nithin", msg, hist, count),
|
| 124 |
+
inputs=[msg1, state, counter],
|
| 125 |
+
outputs=[chatbot, msg1, state, counter, chroma_suggestions, llm_suggestion]
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
send2.click(
|
| 129 |
+
fn=lambda msg, hist, count: send_message("Nithish", msg, hist, count),
|
| 130 |
+
inputs=[msg2, state, counter],
|
| 131 |
+
outputs=[chatbot, msg2, state, counter, chroma_suggestions, llm_suggestion]
|
| 132 |
+
)
|
| 133 |
|
| 134 |
+
demo.launch()
|
|
|