Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,29 @@
|
|
| 1 |
-
import
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
dataset = None
|
| 11 |
-
|
| 12 |
-
if os.path.exists(DATA_PATH):
|
| 13 |
-
import pandas as pd
|
| 14 |
-
dataset = pd.read_csv(DATA_PATH)
|
| 15 |
-
print(f"✅ Loaded dataset locally: {DATA_PATH} ({len(dataset)} rows)")
|
| 16 |
-
else:
|
| 17 |
-
try:
|
| 18 |
-
from datasets import load_dataset
|
| 19 |
-
dataset = load_dataset("your-username/university-ranking-uk") # upload to HF Hub first
|
| 20 |
-
print("✅ Loaded dataset from Hugging Face Hub")
|
| 21 |
-
except Exception as e:
|
| 22 |
-
print("⚠ Could not load dataset:", e)
|
| 23 |
-
dataset = None
|
| 24 |
-
|
| 25 |
-
# -------------------------
|
| 26 |
-
# CHATBOT LOGIC
|
| 27 |
-
# -------------------------
|
| 28 |
client = InferenceClient("microsoft/phi-4")
|
| 29 |
|
| 30 |
def respond(message, history):
|
| 31 |
-
messages = [
|
| 32 |
-
{
|
| 33 |
-
"role": "system",
|
| 34 |
-
"content": (
|
| 35 |
-
"You are a realistic and friendly career advisor to help secondary school students "
|
| 36 |
-
"with important decisions such as the university courses they should apply to, "
|
| 37 |
-
"careers to pursue, etc. Use the dataset to give accurate advice when possible."
|
| 38 |
-
)
|
| 39 |
-
}
|
| 40 |
-
]
|
| 41 |
|
| 42 |
if history:
|
| 43 |
messages.extend(history)
|
| 44 |
|
| 45 |
-
messages.append({"role": "user", "content":
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
results = matches[matches.apply(lambda row: query in row.to_string().lower(), axis=1)]
|
| 54 |
-
if not results.empty:
|
| 55 |
-
messages.append({
|
| 56 |
-
"role": "assistant",
|
| 57 |
-
"content": f"I found {len(results)} matching rows in the ranking dataset."
|
| 58 |
-
})
|
| 59 |
-
except Exception as e:
|
| 60 |
-
print("⚠ Dataset search failed:", e)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
return response["choices"][0]["message"]["content"].strip()
|
| 64 |
|
| 65 |
-
# -
|
| 66 |
-
|
| 67 |
-
# -------------------------
|
| 68 |
-
chatbot = gr.ChatInterface(
|
| 69 |
-
respond,
|
| 70 |
-
type="messages",
|
| 71 |
-
title="CASSI"
|
| 72 |
-
)
|
| 73 |
-
chatbot.launch()
|
|
|
|
| 1 |
+
# import random
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
+
import kagglehub
|
| 5 |
+
# Download latest version
|
| 6 |
+
path = kagglehub.dataset_download("vishaljiodedra/university-ranking-in-the-uk")
|
| 7 |
+
print("Path to dataset files:", path)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
client = InferenceClient("microsoft/phi-4")
|
| 10 |
|
| 11 |
def respond(message, history):
|
| 12 |
+
messages = [{"role": "system", "content": "you are a realistic and friendly career advisor to help secondary school students with important decisions such as the university courses they should apply to, careers to pursue, etc. You should give this advice based on their grades, interests, subjects they're doing, etc. Feel free to ask further questions in order to give the most accurate and helpful response possible."}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
if history:
|
| 15 |
messages.extend(history)
|
| 16 |
|
| 17 |
+
messages.append({"role": "user", "content":message})
|
| 18 |
|
| 19 |
+
|
| 20 |
+
response = client.chat_completion(
|
| 21 |
+
messages,
|
| 22 |
+
max_tokens=500
|
| 23 |
+
)
|
| 24 |
+
return response['choices'][0]['message']['content'].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
|
|
|
|
| 27 |
|
| 28 |
+
chatbot = gr.ChatInterface(respond, type = "messages", title = "CASSI") #chatbot ui - conversation history and user input
|
| 29 |
+
chatbot.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|