Spaces:
Runtime error
Runtime error
Commit ·
5099223
1
Parent(s): 8f43716
Add : Recommendation system
Browse files- app.py +105 -3
- cleaned_data.csv +0 -0
- requirements.txt +6 -0
app.py
CHANGED
|
@@ -1,7 +1,109 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
import ast
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from geopy.geocoders import Nominatim
|
| 6 |
+
from geopy.distance import geodesic
|
| 7 |
+
from geopy.exc import GeocoderTimedOut
|
| 8 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 9 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 10 |
+
from google import genai
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
import os
|
| 13 |
|
| 14 |
+
# --- Load API Key dari .env ---
|
| 15 |
+
load_dotenv()
|
| 16 |
+
API_KEY = os.getenv("GEMINI_API_KEY")
|
| 17 |
+
genai.configure(api_key=API_KEY)
|
| 18 |
+
|
| 19 |
+
# --- Load Data dari cleaned_data.csv ---
|
| 20 |
+
try:
|
| 21 |
+
df = pd.read_csv("cleaned_data.csv")
|
| 22 |
+
except FileNotFoundError:
|
| 23 |
+
print("File cleaned_data.csv tidak ditemukan.")
|
| 24 |
+
df = pd.DataFrame(columns=['nama', 'description', 'latitude', 'longitude'])
|
| 25 |
+
|
| 26 |
+
# --- Fungsi Ekstraksi Kata Kunci ---
|
| 27 |
+
def extract_keywords(user_input):
|
| 28 |
+
prompt = f"""
|
| 29 |
+
Ekstrak 3–7 kata kunci penting dari deskripsi wisata berikut:
|
| 30 |
+
"{user_input}"
|
| 31 |
+
Tulis langsung sebagai list Python tanpa variabel apapun.
|
| 32 |
+
"""
|
| 33 |
+
try:
|
| 34 |
+
response = genai.GenerativeModel("gemini-1.5-flash").generate_content(prompt)
|
| 35 |
+
matches = re.findall(r'\[.*?\]', response.text)
|
| 36 |
+
if matches:
|
| 37 |
+
return ast.literal_eval(matches[0])
|
| 38 |
+
else:
|
| 39 |
+
return []
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return [f"Error: {e}"]
|
| 42 |
+
|
| 43 |
+
# --- Fungsi Lokasi ---
|
| 44 |
+
def get_coordinates_from_location(location_name):
|
| 45 |
+
try:
|
| 46 |
+
geolocator = Nominatim(user_agent="geoapi")
|
| 47 |
+
location = geolocator.geocode(location_name, timeout=10)
|
| 48 |
+
return (location.latitude, location.longitude) if location else (None, None)
|
| 49 |
+
except GeocoderTimedOut:
|
| 50 |
+
return (None, None)
|
| 51 |
+
|
| 52 |
+
def get_location_name_from_coordinates(lat, lon):
|
| 53 |
+
try:
|
| 54 |
+
geolocator = Nominatim(user_agent="geoapi")
|
| 55 |
+
location = geolocator.reverse((lat, lon), timeout=10)
|
| 56 |
+
return location.address if location else "Tidak ditemukan"
|
| 57 |
+
except GeocoderTimedOut:
|
| 58 |
+
return "Tidak ditemukan"
|
| 59 |
+
|
| 60 |
+
# --- Rekomendasi Tempat Wisata ---
|
| 61 |
+
def prepare_and_recommend(df, user_description):
|
| 62 |
+
tfidf = TfidfVectorizer()
|
| 63 |
+
tfidf_matrix = tfidf.fit_transform(df['description'].astype(str).tolist() + [user_description])
|
| 64 |
+
similarity = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1]).flatten()
|
| 65 |
+
df['similarity'] = similarity
|
| 66 |
+
return df.sort_values(by='similarity', ascending=False).head(5)
|
| 67 |
+
|
| 68 |
+
def sort_by_nearest_location(df, user_lat, user_lon):
|
| 69 |
+
df['distance_km'] = df.apply(
|
| 70 |
+
lambda row: geodesic((user_lat, user_lon), (row['latitude'], row['longitude'])).km,
|
| 71 |
+
axis=1
|
| 72 |
+
)
|
| 73 |
+
return df.sort_values(by='distance_km')
|
| 74 |
+
|
| 75 |
+
# --- Fungsi Utama Gradio ---
|
| 76 |
+
def wisata_rekomendasi(deskripsi, lokasi):
|
| 77 |
+
if df.empty:
|
| 78 |
+
return "Data wisata tidak tersedia. Pastikan cleaned_data.csv ada."
|
| 79 |
+
|
| 80 |
+
keywords = extract_keywords(deskripsi)
|
| 81 |
+
if "Error:" in str(keywords):
|
| 82 |
+
return str(keywords[0])
|
| 83 |
+
|
| 84 |
+
user_description_joined = " ".join(keywords)
|
| 85 |
+
lat, lon = get_coordinates_from_location(lokasi)
|
| 86 |
+
if lat is None or lon is None:
|
| 87 |
+
return "Lokasi tidak ditemukan."
|
| 88 |
+
|
| 89 |
+
reverse_location = get_location_name_from_coordinates(lat, lon)
|
| 90 |
+
top_place = prepare_and_recommend(df.copy(), user_description_joined)
|
| 91 |
+
sorted_place = sort_by_nearest_location(top_place, lat, lon)
|
| 92 |
+
|
| 93 |
+
hasil = f"Lokasi terdekat dari {reverse_location}:\n\n"
|
| 94 |
+
hasil += sorted_place[['nama', 'distance_km', 'description']].to_string(index=False)
|
| 95 |
+
return hasil
|
| 96 |
+
|
| 97 |
+
# --- UI Gradio ---
|
| 98 |
+
demo = gr.Interface(
|
| 99 |
+
fn=wisata_rekomendasi,
|
| 100 |
+
inputs=[
|
| 101 |
+
gr.Textbox(label="Deskripsi Wisata yang Anda Inginkan"),
|
| 102 |
+
gr.Textbox(label="Lokasi Anda (Contoh: Gunung Kidul, Yogyakarta)")
|
| 103 |
+
],
|
| 104 |
+
outputs="text",
|
| 105 |
+
title="Sistem Rekomendasi Wisata",
|
| 106 |
+
description="Masukkan deskripsi dan lokasi, lalu dapatkan rekomendasi tempat wisata terdekat"
|
| 107 |
+
)
|
| 108 |
|
|
|
|
| 109 |
demo.launch()
|
cleaned_data.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
scikit-learn
|
| 4 |
+
geopy
|
| 5 |
+
google-generativeai
|
| 6 |
+
python-dotenv
|