Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,23 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
import os
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
if prompt:
|
| 23 |
-
with st.spinner("Generating Python code using AI..."):
|
| 24 |
-
code = generate_code(prompt)
|
| 25 |
-
st.code(code, language="python")
|
| 26 |
-
|
| 27 |
-
try:
|
| 28 |
-
exec_globals = {"df": df}
|
| 29 |
-
exec(code, exec_globals)
|
| 30 |
-
df = exec_globals["df"]
|
| 31 |
-
st.success("✅ Data updated successfully!")
|
| 32 |
-
st.write(df.head())
|
| 33 |
-
|
| 34 |
-
# Allow download
|
| 35 |
-
csv = df.to_csv(index=False).encode('utf-8')
|
| 36 |
-
st.download_button("⬇️ Download Updated CSV", csv, "updated_data.csv", "text/csv")
|
| 37 |
-
except Exception as e:
|
| 38 |
-
st.error(f"⚠️ Error while executing AI-generated code: {e}")
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Initialize Hugging Face client
|
| 5 |
+
client = InferenceClient(
|
| 6 |
+
model="Salesforce/codegen-2B-multi",
|
| 7 |
+
token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 8 |
+
)
|
| 9 |
|
| 10 |
+
def generate_code(prompt):
|
| 11 |
+
full_prompt = f"""You are a professional Python Data Scientist.
|
| 12 |
+
Given the following instruction, write Python pandas code to perform the task on a dataframe named df.
|
| 13 |
+
Instruction: {prompt}
|
| 14 |
+
Output only valid Python code. No explanations."""
|
| 15 |
|
| 16 |
+
response = client.text_generation(
|
| 17 |
+
prompt=full_prompt,
|
| 18 |
+
max_new_tokens=300,
|
| 19 |
+
temperature=0.2,
|
| 20 |
+
top_p=0.95,
|
| 21 |
+
repetition_penalty=1.1
|
| 22 |
+
)
|
| 23 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|