yigitcanozdemir
commited on
Commit
·
093199c
1
Parent(s):
676beda
Update for gradio api usage
Browse files- .gitignore +1 -0
- app.py +6 -1
- components/gradio_ui.py +45 -43
.gitignore
CHANGED
|
@@ -161,3 +161,4 @@ cython_debug/
|
|
| 161 |
|
| 162 |
# Exclude trained models
|
| 163 |
=*.0*
|
|
|
|
|
|
| 161 |
|
| 162 |
# Exclude trained models
|
| 163 |
=*.0*
|
| 164 |
+
.old_snippets
|
app.py
CHANGED
|
@@ -9,7 +9,12 @@ def main():
|
|
| 9 |
|
| 10 |
interface = create_interface(engine)
|
| 11 |
|
| 12 |
-
interface.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
if __name__ == "__main__":
|
|
|
|
| 9 |
|
| 10 |
interface = create_interface(engine)
|
| 11 |
|
| 12 |
+
interface.launch(
|
| 13 |
+
share=True,
|
| 14 |
+
server_name="0.0.0.0",
|
| 15 |
+
server_port=7860,
|
| 16 |
+
show_api=True,
|
| 17 |
+
)
|
| 18 |
|
| 19 |
|
| 20 |
if __name__ == "__main__":
|
components/gradio_ui.py
CHANGED
|
@@ -1,50 +1,52 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from components.imdb_poster import get_imdb_poster
|
| 3 |
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
history.append([message, response_text])
|
| 33 |
-
|
| 34 |
-
return history, "", thumbnails
|
| 35 |
-
|
| 36 |
-
except Exception as e:
|
| 37 |
-
history.append([message, f"❌ Error: {str(e)}"])
|
| 38 |
-
return history, "", []
|
| 39 |
-
|
| 40 |
-
with gr.Blocks() as demo:
|
| 41 |
-
with gr.Column():
|
| 42 |
-
chatbot = gr.Chatbot(height=600)
|
| 43 |
-
gallery = gr.Gallery(
|
| 44 |
-
label="Posters", show_label=False, columns=5, height=400
|
| 45 |
)
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
msg.submit(chat_function, [msg, chatbot], [chatbot, msg, gallery])
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from models.recommendation_engine import RecommendationEngine
|
| 3 |
from components.imdb_poster import get_imdb_poster
|
| 4 |
|
| 5 |
|
| 6 |
+
def get_recommendations_api(message, engine):
|
| 7 |
+
if not message:
|
| 8 |
+
return []
|
| 9 |
+
|
| 10 |
+
try:
|
| 11 |
+
result = engine.get_recommendations(message)
|
| 12 |
+
df = result[1] if isinstance(result, tuple) and len(result) > 1 else None
|
| 13 |
+
if df is None or df.empty:
|
| 14 |
+
return []
|
| 15 |
+
|
| 16 |
+
recommendations = []
|
| 17 |
+
for _, row in df.iterrows():
|
| 18 |
+
poster_url = get_imdb_poster(row["ImdbId"])
|
| 19 |
+
recommendations.append(
|
| 20 |
+
{
|
| 21 |
+
"imdb_id": row["ImdbId"],
|
| 22 |
+
"title": row["Title"],
|
| 23 |
+
"year": row["Year"],
|
| 24 |
+
"type": row["Type"],
|
| 25 |
+
"rating": row["Rating"],
|
| 26 |
+
"votes": row["Votes"],
|
| 27 |
+
"genres": row["Genres"],
|
| 28 |
+
"similarity": row["Similarity"],
|
| 29 |
+
"hybrid_score": row["Hybrid Score"],
|
| 30 |
+
"overview": row["Overview"],
|
| 31 |
+
"poster_url": poster_url,
|
| 32 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
+
return recommendations
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Error getting recommendations: {e}")
|
| 37 |
+
return []
|
| 38 |
|
|
|
|
| 39 |
|
| 40 |
+
def create_interface(engine):
|
| 41 |
+
def predict_wrapper(message):
|
| 42 |
+
return get_recommendations_api(message, engine)
|
| 43 |
+
|
| 44 |
+
iface = gr.Interface(
|
| 45 |
+
fn=predict_wrapper,
|
| 46 |
+
inputs=gr.Textbox(lines=1, placeholder="Type your movie query..."),
|
| 47 |
+
outputs=gr.JSON(label="Recommendations"),
|
| 48 |
+
title="Movie Recommendation API",
|
| 49 |
+
description="Type a movie or genre, get recommendations with posters.",
|
| 50 |
+
api_name="predict",
|
| 51 |
+
)
|
| 52 |
+
return iface
|