leofltt commited on
Commit
95afeec
·
1 Parent(s): 81917a3

initial impl

Browse files
Files changed (2) hide show
  1. app.py +94 -4
  2. requirements.txt +9 -1
app.py CHANGED
@@ -3,6 +3,13 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
 
 
 
 
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -13,11 +20,94 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
  class BasicAgent:
14
  def __init__(self):
15
  print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from llama_index import VectorStoreIndex, SimpleDirectoryReader, Document
7
+ from llama_index.llms import HuggingFaceLLM
8
+ from llama_index import ServiceContext
9
+ from llama_index.embeddings import HuggingFaceEmbedding
10
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
11
+ import torch
12
+ from dotenv import load_dotenv
13
 
14
  # (Keep Constants as is)
15
  # --- Constants ---
 
20
  class BasicAgent:
21
  def __init__(self):
22
  print("BasicAgent initialized.")
23
+ load_dotenv()
24
+
25
+ # Initialize model and tokenizer
26
+ model_name = "open-r1/OlympicCoder-7B"
27
+
28
+ # Configure quantization
29
+ quantization_config = BitsAndBytesConfig(
30
+ load_in_4bit=True,
31
+ bnb_4bit_compute_dtype=torch.float16,
32
+ bnb_4bit_quant_type="nf4",
33
+ bnb_4bit_use_double_quant=True,
34
+ )
35
+
36
+ # Initialize tokenizer and model
37
+ self.tokenizer = AutoTokenizer.from_pretrained(model_name)
38
+ self.model = AutoModelForCausalLM.from_pretrained(
39
+ model_name,
40
+ device_map="auto",
41
+ quantization_config=quantization_config,
42
+ trust_remote_code=True
43
+ )
44
+
45
+ # Create HuggingFaceLLM instance
46
+ self.llm = HuggingFaceLLM(
47
+ context_window=4096,
48
+ max_new_tokens=512,
49
+ tokenizer=self.tokenizer,
50
+ model=self.model,
51
+ model_name=model_name,
52
+ device_map="auto",
53
+ temperature=0.1
54
+ )
55
+
56
+ print("Agent initialized.")
57
+
58
  def __call__(self, question: str) -> str:
59
+ print(f"Agent received question: {question[:50]}...")
60
+
61
+ try:
62
+ # Extract data from the question if present
63
+ data_start = question.find('```python')
64
+ if data_start != -1:
65
+ data_end = question.find('```', data_start + 8)
66
+ data_code = question[data_start + 8:data_end].strip()
67
+
68
+ # Execute the data code in a safe context to create DataFrame
69
+ local_vars = {}
70
+ exec(data_code, {"pd": pd}, local_vars)
71
+ df = local_vars.get('df')
72
+
73
+ # Extract the actual question (usually after the code block)
74
+ actual_question = question[data_end + 3:].strip()
75
+
76
+ # Create analysis prompt
77
+ prompt = f"""
78
+ You are a data analysis expert. Given the following DataFrame and question,
79
+ provide the correct answer using pandas operations.
80
+
81
+ DataFrame head:
82
+ {df.head().to_string()}
83
+
84
+ DataFrame info:
85
+ {df.info()}
86
+
87
+ Question: {actual_question}
88
+
89
+ Provide only the final answer without explanations.
90
+ """
91
+
92
+ else:
93
+ # Handle non-data questions
94
+ prompt = f"""
95
+ You are a helpful AI assistant specializing in data analysis.
96
+ Please provide a clear, concise answer to this question:
97
+ {question}
98
+ """
99
+
100
+ # Use LlamaIndex query engine
101
+ query_engine = self.index.as_query_engine()
102
+ response = query_engine.query(prompt)
103
+ answer = str(response)
104
+
105
+ print(f"Agent generated answer: {answer[:100]}...")
106
+ return answer
107
+
108
+ except Exception as e:
109
+ print(f"Error generating answer: {e}")
110
+ return f"I apologize, I encountered an error: {str(e)}"
111
 
112
  def run_and_submit_all( profile: gr.OAuthProfile | None):
113
  """
requirements.txt CHANGED
@@ -1,2 +1,10 @@
1
  gradio
2
- requests
 
 
 
 
 
 
 
 
 
1
  gradio
2
+ requests
3
+ llama-index
4
+ transformers
5
+ python-dotenv
6
+ torch
7
+ sentence-transformers
8
+ nltk
9
+ accelerate
10
+ bitsandbytes