Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,3 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
from smolagents import CodeAgent, tool
|
4 |
-
from groq import Groq
|
5 |
-
import os
|
6 |
-
|
7 |
-
class GroqLLM:
|
8 |
-
def __init__(self, model_name="llama-3.1-8B-Instant"):
|
9 |
-
self.client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
10 |
-
self.model_name = model_name
|
11 |
-
|
12 |
-
def __call__(self, prompt: str):
|
13 |
-
completion = self.client.chat.completions.create(
|
14 |
-
model=self.model_name,
|
15 |
-
messages=[{"role": "user", "content": prompt}],
|
16 |
-
temperature=0.7,
|
17 |
-
max_tokens=1024,
|
18 |
-
stream=False,
|
19 |
-
)
|
20 |
-
return completion.choices[0].message.content
|
21 |
-
|
22 |
@tool
|
23 |
def execute_code(code_string: str, data: pd.DataFrame) -> str:
|
24 |
"""Executes python code and returns results as a string.
|
@@ -29,26 +8,4 @@ def execute_code(code_string: str, data: pd.DataFrame) -> str:
|
|
29 |
Returns:
|
30 |
str: The result of executing the code or an error message
|
31 |
"""
|
32 |
-
|
33 |
-
local_vars = {"data": data, "pd": pd}
|
34 |
-
exec(code_string, local_vars)
|
35 |
-
if "result" in local_vars:
|
36 |
-
return str(local_vars["result"])
|
37 |
-
return "Success, but no 'result' variable assigned."
|
38 |
-
except Exception as e:
|
39 |
-
return f"Error: {e}"
|
40 |
-
|
41 |
-
def main():
|
42 |
-
st.title("Test")
|
43 |
-
data = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
|
44 |
-
|
45 |
-
agent = CodeAgent(
|
46 |
-
tools=[execute_code],
|
47 |
-
model=GroqLLM(),
|
48 |
-
)
|
49 |
-
code = "result = data.sum()"
|
50 |
-
result = agent.run(f"Use the execute_code tool, run this python code ```python\n{code}\n```", data=data)
|
51 |
-
st.write(result)
|
52 |
-
|
53 |
-
if __name__ == "__main__":
|
54 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
@tool
|
2 |
def execute_code(code_string: str, data: pd.DataFrame) -> str:
|
3 |
"""Executes python code and returns results as a string.
|
|
|
8 |
Returns:
|
9 |
str: The result of executing the code or an error message
|
10 |
"""
|
11 |
+
return f"Received code: {code_string}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|