Rashmi0801 commited on
Commit
1a6274b
·
verified ·
1 Parent(s): 200d8cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -144
app.py CHANGED
@@ -1,165 +1,151 @@
1
  api_key = "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"
2
 
3
- import streamlit as st
4
- from langchain_groq import ChatGroq
5
- from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
6
- from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
7
- from langchain.agents import initialize_agent, AgentType
8
  import os
 
9
  import requests
10
- import pandas as pd
11
  from dotenv import load_dotenv
 
12
 
13
  # Load environment variables
14
  load_dotenv()
15
 
16
- # Constants for Basic Agent Evaluation
17
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Initialize search tools (with warm-up)
20
- @st.cache_resource
21
- def load_tools():
22
- with st.spinner("Initializing tools (first time may take a few seconds)..."):
23
- api_wrapper_arxiv = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=250)
24
- arxiv = ArxivQueryRun(api_wrapper=api_wrapper_arxiv)
25
- api_wrapper_wiki = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=250)
26
- wiki = WikipediaQueryRun(api_wrapper=api_wrapper_wiki)
27
- search = DuckDuckGoSearchRun(name="Search")
28
- # Warm up tools
29
- arxiv.run("machine learning")
30
- wiki.run("machine learning")
31
- return [search, arxiv, wiki]
32
-
33
- tools = load_tools()
34
-
35
- # Streamlit app layout
36
- st.title("Langchain - Chat with Search & Evaluation")
37
-
38
- # Sidebar for settings
39
- st.sidebar.title("Settings")
40
- api_key = st.sidebar.text_input("Enter your Groq API Key:", type="password")
41
-
42
- # Initialize chat messages
43
- if "messages" not in st.session_state:
44
- st.session_state["messages"] = [
45
- {"role": "assistant", "content": "Hi, I am a Chatbot who can search the web and evaluate questions. How can I help you?"}
46
- ]
47
-
48
- # Display chat messages
49
- for msg in st.session_state.messages:
50
- st.chat_message(msg["role"]).write(msg["content"])
51
-
52
- # Chat input
53
- if prompt := st.chat_input(placeholder="What is machine learning?"):
54
- st.session_state.messages.append({"role": "user", "content": prompt})
55
- st.chat_message("user").write(prompt)
56
-
57
- if not api_key:
58
- st.error("Please enter your Groq API key in the sidebar.")
59
- st.stop()
60
-
61
- llm = ChatGroq(groq_api_key=api_key, model_name="llama3-70b-8192")
62
- search_agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True)
63
-
64
- with st.chat_message("assistant"):
65
- response = search_agent.run(st.session_state.messages)
66
- st.session_state.messages.append({'role': 'assistant', "content": response})
67
- st.write(response)
68
-
69
- # Basic Agent Evaluation Section
70
- st.sidebar.title("Basic Agent Evaluation")
71
-
72
- def run_evaluation():
73
- """Function to run the evaluation with progress updates"""
74
- if not api_key:
75
- st.error("Please enter your Groq API key in the sidebar.")
76
- return "API key required", pd.DataFrame()
77
-
78
- # Setup progress tracking
79
- progress_bar = st.sidebar.progress(0)
80
- status_text = st.sidebar.empty()
81
- results_container = st.empty()
82
-
83
- username = "streamlit_user"
84
- api_url = DEFAULT_API_URL
85
- questions_url = f"{api_url}/questions"
86
- submit_url = f"{api_url}/submit"
87
- space_id = os.getenv("SPACE_ID", "local")
88
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id != "local" else "local_execution"
89
-
90
  try:
91
- # 1. Fetch Questions
92
- status_text.text("📡 Fetching questions...")
93
- response = requests.get(questions_url, timeout=15)
94
- response.raise_for_status()
95
  questions_data = response.json()
96
- total_questions = len(questions_data)
97
- status_text.text(f"✅ Found {total_questions} questions")
98
-
99
  if not questions_data:
100
- return "No questions found", pd.DataFrame()
101
-
102
- # 2. Initialize Agent (reuse tools from cache)
103
- llm = ChatGroq(groq_api_key=api_key, model_name="llama3-70b-8192")
104
- agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True)
105
-
106
- # 3. Process Questions
107
- results_log = []
108
- answers_payload = []
109
-
110
- for i, item in enumerate(questions_data):
111
- progress = (i + 1) / total_questions
112
- progress_bar.progress(progress)
113
- status_text.text(f"🔍 Processing question {i+1}/{total_questions}...")
114
 
115
- task_id = item.get("task_id")
116
- question_text = item.get("question")
117
- if not task_id or not question_text:
118
- continue
119
-
120
- try:
121
- submitted_answer = agent.run(question_text)
122
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
123
- results_log.append({"Task ID": task_id, "Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
124
- "Submitted Answer": submitted_answer[:200] + "..." if len(submitted_answer) > 200 else submitted_answer})
125
-
126
- # Update results table progressively
127
- if (i + 1) % 3 == 0 or (i + 1) == total_questions: # Update every 3 questions or at end
128
- results_container.dataframe(pd.DataFrame(results_log))
129
- except Exception as e:
130
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"❌ Error: {str(e)}"})
131
-
132
- # 4. Submit Answers
133
- status_text.text("📤 Submitting answers...")
134
- submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
135
- response = requests.post(submit_url, json=submission_data, timeout=60)
136
- response.raise_for_status()
137
- result_data = response.json()
 
138
 
139
- final_status = (
140
- f"✅ Submission Successful!\n"
141
- f"📊 Score: {result_data.get('score', 'N/A')}%\n"
142
- f"📝 Correct: {result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')}\n"
143
- f"💬 Message: {result_data.get('message', 'No message')}"
 
144
  )
145
- return final_status, pd.DataFrame(results_log)
146
-
147
  except Exception as e:
148
- return f" Failed: {str(e)}", pd.DataFrame(results_log if 'results_log' in locals() else [])
149
-
150
- finally:
151
- progress_bar.empty()
152
- status_text.empty()
153
-
154
- # Evaluation button in sidebar
155
- if st.sidebar.button("🚀 Run Evaluation & Submit Answers"):
156
- with st.spinner("Starting evaluation..."):
157
- status, results = run_evaluation()
158
 
159
- st.sidebar.success("Evaluation completed!")
160
- st.sidebar.text_area("Results", value=status, height=150)
161
 
162
- if not results.empty:
163
- st.subheader("📋 Detailed Results")
164
- st.dataframe(results, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
165
 
 
1
  api_key = "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"
2
 
 
 
 
 
 
3
  import os
4
+ import gradio as gr
5
  import requests
6
+ from huggingface_hub import InferenceClient, login
7
  from dotenv import load_dotenv
8
+ import pandas as pd
9
 
10
  # Load environment variables
11
  load_dotenv()
12
 
13
+ # Constants
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
+ MODEL_NAME = "meta-llama/llama-4-maverick-17b-128e-instruct"
16
+
17
+ # Initialize the Llama Maverick client
18
+ class MaverickAgent:
19
+ def __init__(self):
20
+ try:
21
+ self.client = InferenceClient(
22
+ model=MODEL_NAME,
23
+ token=os.getenv("HUGGINGFACE_TOKEN")
24
+ )
25
+ print("MaverickAgent initialized successfully")
26
+ except Exception as e:
27
+ print(f"Error initializing MaverickAgent: {e}")
28
+ raise
29
+
30
+ def __call__(self, question: str) -> str:
31
+ try:
32
+ print(f"Processing question: {question[:100]}...")
33
+
34
+ # Custom prompt template for the Maverick model
35
+ prompt = f"""<|begin_of_text|>
36
+ <|start_header_id|>system<|end_header_id|>
37
+ You are an AI assistant that provides accurate and concise answers to questions.
38
+ Be factual and respond with just the answer unless asked to elaborate.
39
+ <|eot_id|>
40
+ <|start_header_id|>user<|end_header_id|>
41
+ {question}
42
+ <|eot_id|>
43
+ <|start_header_id|>assistant<|end_header_id|>"""
44
+
45
+ response = self.client.text_generation(
46
+ prompt,
47
+ max_new_tokens=256,
48
+ temperature=0.7,
49
+ do_sample=True,
50
+ )
51
+
52
+ # Clean up the response
53
+ answer = response.split("<|eot_id|>")[0].strip()
54
+ print(f"Generated answer: {answer[:200]}...")
55
+ return answer
56
+ except Exception as e:
57
+ print(f"Error processing question: {e}")
58
+ return f"Error: {str(e)}"
59
+
60
+ # Authentication
61
+ try:
62
+ login(token=os.getenv("HUGGINGFACE_TOKEN"))
63
+ except Exception as e:
64
+ print(f"Authentication error: {e}")
65
+
66
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
67
+ if not profile:
68
+ return "Please log in with Hugging Face first.", None
69
+
70
+ # Initialize agent
71
+ try:
72
+ agent = MaverickAgent()
73
+ except Exception as e:
74
+ return f"Agent initialization failed: {e}", None
75
 
76
+ # Fetch questions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  try:
78
+ response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
 
 
 
79
  questions_data = response.json()
 
 
 
80
  if not questions_data:
81
+ return "No questions available.", None
82
+ except Exception as e:
83
+ return f"Failed to fetch questions: {e}", None
84
+
85
+ # Process questions
86
+ results = []
87
+ answers = []
88
+ for i, item in enumerate(questions_data):
89
+ task_id = item.get("task_id")
90
+ question = item.get("question")
91
+ if not task_id or not question:
92
+ continue
 
 
93
 
94
+ try:
95
+ answer = agent(question)
96
+ answers.append({"task_id": task_id, "submitted_answer": answer})
97
+ results.append({
98
+ "Task ID": task_id,
99
+ "Question": question[:100] + "..." if len(question) > 100 else question,
100
+ "Answer": answer[:100] + "..." if len(answer) > 100 else answer
101
+ })
102
+ except Exception as e:
103
+ results.append({
104
+ "Task ID": task_id,
105
+ "Question": question,
106
+ "Answer": f"Error: {str(e)}"
107
+ })
108
+
109
+ # Submit answers
110
+ try:
111
+ submission = {
112
+ "username": profile.username,
113
+ "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}",
114
+ "answers": answers
115
+ }
116
+ response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
117
+ result = response.json()
118
 
119
+ return (
120
+ f"✅ Submitted {len(answers)} answers\n"
121
+ f"📊 Score: {result.get('score', 'N/A')}%\n"
122
+ f"🔢 Correct: {result.get('correct_count', 0)}/{len(answers)}\n"
123
+ f"🤖 Model: {MODEL_NAME}",
124
+ pd.DataFrame(results)
125
  )
 
 
126
  except Exception as e:
127
+ return f"Submission failed: {e}", pd.DataFrame(results)
128
+
129
+ # Gradio Interface
130
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
131
+ gr.Markdown("# 🦙 Llama 4 Maverick Agent")
132
+ gr.Markdown(f"Using `{MODEL_NAME}` for evaluation")
 
 
 
 
133
 
134
+ gr.LoginButton()
 
135
 
136
+ with gr.Row():
137
+ run_btn = gr.Button("Run Evaluation", variant="primary")
138
+
139
+ with gr.Row():
140
+ status = gr.Textbox(label="Status", interactive=False)
141
+ results = gr.DataFrame(label="Results", wrap=True)
142
+
143
+ run_btn.click(
144
+ fn=run_and_submit_all,
145
+ outputs=[status, results]
146
+ )
147
+
148
+ if __name__ == "__main__":
149
+ demo.launch()
150
+
151