reddmann007 commited on
Commit
beca749
Β·
verified Β·
1 Parent(s): a72d349

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -2,22 +2,27 @@ import os
2
  import gradio as gr
3
  from smolagents import CodeAgent, tool, LiteLLMModel
4
  from qdrant_client import QdrantClient
5
- from qdrant_client.models import PointStruct, VectorParams, Distance, Filter, FieldCondition, MatchValue
6
  from sentence_transformers import SentenceTransformer
7
 
8
- # --- 1. Setup Connections ---
9
- # Ensure these match your Hugging Face Secrets names exactly
10
  client = QdrantClient(
11
  url=os.getenv("QDRANT_URL"),
12
  api_key=os.getenv("QDRANT_API_KEY")
13
  )
 
14
  embed_model = SentenceTransformer("all-MiniLM-L6-v2")
15
  collection_name = "cricket_analysis"
16
-
17
- # Starter Match Configuration
18
- STARTER_URL = "https://www.espncricinfo.com/series/icc-men-s-t20-world-cup-2024-1411166/south-africa-vs-india-final-1415755/ball-by-ball-commentary"
19
  STARTER_ID = "T20_WC_Final_2024"
20
 
 
 
 
 
 
 
 
21
  # --- 2. The Search Tool ---
22
  @tool
23
  def search_cricket_database(query: str, active_match_id: str) -> str:
@@ -43,36 +48,45 @@ def search_cricket_database(query: str, active_match_id: str) -> str:
43
 
44
  return "\n\n".join([f"[Over {r.payload.get('over_number')}] {r.payload['text']}" for r in results])
45
 
46
- # --- 3. The Agent Brain ---
47
- # We define our professional rules here
48
- analyst_instructions = """
49
- You are a Lead Cricket Strategy Analyst. Your tone is professional and data-driven.
50
- Always use the 'search_cricket_database' tool to retrieve facts about the match.
51
- """
52
 
53
- # We pass the instructions as the 'description' or part of the first 'run'
54
  agent = CodeAgent(
55
  tools=[search_cricket_database],
56
- model=model,
57
- # Remove system_prompt=system_prompt
58
  )
59
 
60
- # --- 4. Gradio UI Logic ---
61
  def chat_fn(message, history, active_match_id):
62
- prompt = f"Using context from match {active_match_id}: {message}"
63
- # The agent.run command handles the tool calling and Llama logic
64
- response = agent.run(prompt)
 
 
 
 
 
 
 
65
  history.append((message, response))
66
  return "", history
67
 
68
- # --- 5. Interface Layout ---
69
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
70
  gr.Markdown("# 🏏 Elite Cricket Analytics Portal")
71
 
72
- active_id = gr.Textbox(label="Active Match Focus", value=STARTER_ID, interactive=False)
 
 
73
  chatbot = gr.Chatbot(label="Analysis Feed")
74
- msg = gr.Textbox(label="Query the Analyst")
75
 
76
  msg.submit(chat_fn, [msg, chatbot, active_id], [msg, chatbot])
77
 
78
- demo.launch()
 
 
2
  import gradio as gr
3
  from smolagents import CodeAgent, tool, LiteLLMModel
4
  from qdrant_client import QdrantClient
5
+ from qdrant_client.models import Filter, FieldCondition, MatchValue, VectorParams, Distance
6
  from sentence_transformers import SentenceTransformer
7
 
8
+ # --- 1. Connection & Database Setup ---
9
+ # These pull from your 'Secrets' on Hugging Face
10
  client = QdrantClient(
11
  url=os.getenv("QDRANT_URL"),
12
  api_key=os.getenv("QDRANT_API_KEY")
13
  )
14
+
15
  embed_model = SentenceTransformer("all-MiniLM-L6-v2")
16
  collection_name = "cricket_analysis"
 
 
 
17
  STARTER_ID = "T20_WC_Final_2024"
18
 
19
+ # Ensure the collection exists in Qdrant
20
+ if not client.collection_exists(collection_name):
21
+ client.create_collection(
22
+ collection_name=collection_name,
23
+ vectors_config=VectorParams(size=384, distance=Distance.COSINE)
24
+ )
25
+
26
  # --- 2. The Search Tool ---
27
  @tool
28
  def search_cricket_database(query: str, active_match_id: str) -> str:
 
48
 
49
  return "\n\n".join([f"[Over {r.payload.get('over_number')}] {r.payload['text']}" for r in results])
50
 
51
+ # --- 3. Defining the Brain & Agent ---
52
+ # Define the model FIRST to avoid NameError 🧠
53
+ model = LiteLLMModel(
54
+ model_id="meta-llama/Llama-3-70B-Instruct",
55
+ api_key=os.getenv("HF_TOKEN_cric")
56
+ )
57
 
58
+ # Initialize the agent with the model and tools πŸ€–
59
  agent = CodeAgent(
60
  tools=[search_cricket_database],
61
+ model=model
 
62
  )
63
 
64
+ # --- 4. UI Logic ---
65
  def chat_fn(message, history, active_match_id):
66
+ # Professional instructions injected here to maintain the persona πŸŽ™οΈ
67
+ system_instructions = (
68
+ "You are a Lead Cricket Strategy Analyst. Your tone is professional and data-driven. "
69
+ "Use the 'search_cricket_database' tool to retrieve facts about the match. "
70
+ )
71
+
72
+ full_prompt = f"{system_instructions}\n\nUser Question about match {active_match_id}: {message}"
73
+
74
+ # Run the agent and update history
75
+ response = agent.run(full_prompt)
76
  history.append((message, response))
77
  return "", history
78
 
79
+ # --- 5. Gradio Interface Layout ---
80
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
81
  gr.Markdown("# 🏏 Elite Cricket Analytics Portal")
82
 
83
+ with gr.Row():
84
+ active_id = gr.Textbox(label="Active Match Focus", value=STARTER_ID, interactive=False)
85
+
86
  chatbot = gr.Chatbot(label="Analysis Feed")
87
+ msg = gr.Textbox(label="Query the Analyst (e.g., 'How did the powerplay go?')")
88
 
89
  msg.submit(chat_fn, [msg, chatbot, active_id], [msg, chatbot])
90
 
91
+ if __name__ == "__main__":
92
+ demo.launch()