Spaces:
Runtime error
Runtime error
File size: 2,975 Bytes
8936721 f32c5f8 8936721 1ee2dfc 8936721 1ee2dfc 8936721 14f031f 8936721 14f031f 8936721 beca749 14f031f 8936721 14f031f fe8efa5 14f031f 8936721 34f020b 14f031f 34f020b 14f031f 34f020b 14f031f 34f020b 14f031f 34f020b 29b78b8 14f031f 34f020b 3c3b243 14f031f 8936721 14f031f 9b56bcc 14f031f 1ee2dfc 3c3b243 14f031f 34f020b 8936721 1cfb5e6 14f031f 3c3b243 34f020b 9b56bcc 14f031f 8936721 beca749 14f031f | 1 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import os
import gradio as gr
from smolagents import CodeAgent, tool, LiteLLMModel
from qdrant_client import QdrantClient
from qdrant_client.models import Filter, FieldCondition, MatchValue, PointStruct
from sentence_transformers import SentenceTransformer
from firecrawl import FirecrawlApp
# --- Setup ---
client = QdrantClient(url=os.getenv("QDRANT_URL"), api_key=os.getenv("QDRANT_API_KEY"), check_compatibility=False)
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
firecrawl = FirecrawlApp(api_key=os.getenv("FIRECRAWL_API_KEY"))
collection_name = "cricket_analysis"
# --- Tools ---
@tool
def search_cricket_database(query: str, active_match_id: str) -> str:
"""
Search the ball-by-ball database for match events.
Args:
query: The search term about the match.
active_match_id: The ID to filter results.
"""
query_vector = embed_model.encode(query).tolist()
try:
# Try modern query method first
search_result = client.query_points(
collection_name=collection_name,
query=query_vector,
query_filter=Filter(must=[FieldCondition(key="match_id", match=MatchValue(value=active_match_id))]),
limit=5
).points
except AttributeError:
# Fallback for different client versions
search_result = client.search(
collection_name=collection_name,
query_vector=query_vector,
query_filter=Filter(must=[FieldCondition(key="match_id", match=MatchValue(value=active_match_id))]),
limit=5
)
if not search_result: return "No data found."
return "\n\n".join([f"[Context] {r.payload.get('text')}" for r in search_result])
# --- Agent Initialization ---
model = LiteLLMModel(model_id="huggingface/Qwen/Qwen2.5-72B-Instruct", api_key=os.getenv("HF_TOKEN_cric"))
agent = CodeAgent(
tools=[search_cricket_database],
model=model,
# π‘οΈ Allowing the agent to use specific imports if it generates code using them
additional_authorized_imports=["web_search", "math", "time", "re"]
)
# --- UI Logic ---
def chat_fn(message, history, mode, custom_url, custom_id):
active_id = custom_id if mode == "Custom Match" else "T20_WC_Final_2024"
# Run the agent
response = agent.run(f"Match ID: {active_id}. Question: {message}")
# β
History update using dictionary format
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": response})
return "", history
with gr.Blocks() as demo:
gr.Markdown("# π Cricket Analytics Portal")
mode_select = gr.Radio(["Featured Match", "Custom Match"], label="Mode", value="Featured Match")
chatbot = gr.Chatbot(label="Analysis Feed", type="messages")
msg = gr.Textbox(label="Ask the Analyst")
msg.submit(chat_fn, [msg, chatbot, mode_select, gr.State(""), gr.State("")], [msg, chatbot])
if __name__ == "__main__":
demo.launch() |