Spaces:
Running
Running
Jatin Mehra
commited on
Commit
Β·
14a3c08
1
Parent(s):
809aa75
Add Streamlit chat interface for CrawlGPT; implement URL processing, model configuration, and metrics display
Browse files- ui/chat_app.py +121 -0
ui/chat_app.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import asyncio
|
3 |
+
from core.LLMBasedCrawler import Model
|
4 |
+
import os
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from datetime import datetime
|
7 |
+
from core.LLMBasedCrawler import Model
|
8 |
+
from utils.monitoring import MetricsCollector, Metrics
|
9 |
+
from utils.progress import ProgressTracker
|
10 |
+
from utils.data_manager import DataManager
|
11 |
+
from utils.content_validator import ContentValidator
|
12 |
+
|
13 |
+
# Load environment variables
|
14 |
+
load_dotenv()
|
15 |
+
|
16 |
+
# Configure Streamlit page
|
17 |
+
st.set_page_config(
|
18 |
+
page_title="CrawlGPT Chat",
|
19 |
+
page_icon="π€",
|
20 |
+
layout="wide"
|
21 |
+
)
|
22 |
+
|
23 |
+
# Initialize session state
|
24 |
+
if "model" not in st.session_state:
|
25 |
+
st.session_state.model = Model()
|
26 |
+
|
27 |
+
if "messages" not in st.session_state:
|
28 |
+
st.session_state.messages = []
|
29 |
+
|
30 |
+
if "url_processed" not in st.session_state:
|
31 |
+
st.session_state.url_processed = False
|
32 |
+
|
33 |
+
# Helper function to process URL
|
34 |
+
async def process_url(url):
|
35 |
+
success, message = await st.session_state.model.extract_content_from_url(url)
|
36 |
+
return success, message
|
37 |
+
|
38 |
+
# Main UI
|
39 |
+
st.title("CrawlGPT Chat Interface π€")
|
40 |
+
|
41 |
+
# URL Input and Processing
|
42 |
+
with st.sidebar:
|
43 |
+
st.header("π Website Input")
|
44 |
+
url = st.text_input("Enter website URL to analyze:")
|
45 |
+
|
46 |
+
if st.button("Process Website"):
|
47 |
+
if url:
|
48 |
+
with st.spinner("Processing website content..."):
|
49 |
+
success, message = asyncio.run(process_url(url))
|
50 |
+
if success:
|
51 |
+
st.session_state.url_processed = True
|
52 |
+
st.success("β
Website processed successfully!")
|
53 |
+
else:
|
54 |
+
st.error(f"β Error: {message}")
|
55 |
+
else:
|
56 |
+
st.warning("β οΈ Please enter a URL first.")
|
57 |
+
|
58 |
+
# Model Configuration
|
59 |
+
st.header("βοΈ Chat Settings")
|
60 |
+
temperature = st.slider("Temperature", 0.0, 1.0, 0.7)
|
61 |
+
max_tokens = st.slider("Max Tokens", 100, 2000, 500)
|
62 |
+
model_name = st.selectbox(
|
63 |
+
"Model",
|
64 |
+
["llama-3.1-8b-instant", "mixtral-8x7b-32768", "gemma-7b-it"]
|
65 |
+
)
|
66 |
+
use_summary = st.checkbox("Use Summaries", value=True)
|
67 |
+
|
68 |
+
# Display Metrics
|
69 |
+
if st.session_state.url_processed:
|
70 |
+
st.header("π Metrics")
|
71 |
+
metrics = st.session_state.model.metrics_collector.metrics.to_dict()
|
72 |
+
|
73 |
+
col1, col2 = st.columns(2)
|
74 |
+
with col1:
|
75 |
+
st.metric("Total Requests", metrics["total_requests"])
|
76 |
+
st.metric("Successful", metrics["successful_requests"])
|
77 |
+
with col2:
|
78 |
+
st.metric("Avg Response", f"{metrics['average_response_time']:.2f}s")
|
79 |
+
if "total_tokens_used" in metrics:
|
80 |
+
st.metric("Total Tokens", metrics["total_tokens_used"])
|
81 |
+
|
82 |
+
# Chat Interface
|
83 |
+
st.markdown("### π¬ Chat")
|
84 |
+
|
85 |
+
# Display chat messages
|
86 |
+
for message in st.session_state.messages:
|
87 |
+
with st.chat_message(message["role"]):
|
88 |
+
st.markdown(message["content"])
|
89 |
+
|
90 |
+
# Chat input
|
91 |
+
if prompt := st.chat_input("Ask about the website content..."):
|
92 |
+
if not st.session_state.url_processed:
|
93 |
+
st.warning("β οΈ Please process a website first!")
|
94 |
+
else:
|
95 |
+
# Add user message
|
96 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
97 |
+
with st.chat_message("user"):
|
98 |
+
st.markdown(prompt)
|
99 |
+
|
100 |
+
# Generate response
|
101 |
+
with st.chat_message("assistant"):
|
102 |
+
with st.spinner("Thinking..."):
|
103 |
+
response = st.session_state.model.generate_response(
|
104 |
+
query=prompt,
|
105 |
+
temperature=temperature,
|
106 |
+
max_tokens=max_tokens,
|
107 |
+
model=model_name,
|
108 |
+
use_summary=use_summary
|
109 |
+
)
|
110 |
+
st.markdown(response)
|
111 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
112 |
+
|
113 |
+
# Clear chat button
|
114 |
+
if st.session_state.messages:
|
115 |
+
if st.sidebar.button("ποΈ Clear Chat History"):
|
116 |
+
st.session_state.messages = []
|
117 |
+
st.rerun()
|
118 |
+
|
119 |
+
# Debug section
|
120 |
+
if st.sidebar.checkbox("π Show Debug Info"):
|
121 |
+
st.sidebar.json(metrics)
|