Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import matplotlib | |
| matplotlib.use("Agg") | |
| import requests | |
| # Charger les artifacts si présents | |
| try: | |
| df_pricing = pd.read_csv("artifacts/pricing_decisions.csv") | |
| ARTIFACTS_OK = True | |
| except Exception: | |
| df_pricing = pd.DataFrame() | |
| ARTIFACTS_OK = False | |
| def analyze_book(title, reviews_text, avg_units_sold): | |
| if not title or not reviews_text: | |
| return "Please enter a title and at least one review.", "", None | |
| url = "https://matteoadam.app.n8n.cloud/webhook-test/price-decider" | |
| payload = { | |
| "title": title, | |
| "reviews": reviews_text, | |
| "avg_units_sold": avg_units_sold, | |
| } | |
| try: | |
| response = requests.post(url, json=payload, timeout=30) | |
| try: | |
| data = response.json() | |
| except Exception: | |
| return ( | |
| f"Status code: {response.status_code}\nRaw response:\n{response.text}", | |
| "Non-JSON response", | |
| None, | |
| ) | |
| return ( | |
| f"Status code: {response.status_code}\nParsed JSON:\n{data}", | |
| f"Type: {type(data).__name__}", | |
| None, | |
| ) | |
| except Exception as e: | |
| return f"Error: {str(e)}", "Request failed", None | |
| with gr.Blocks(title="Book Price Decider") as app: | |
| gr.Markdown("# Book Price Decider — Group A4") | |
| gr.Markdown("Sentiment analysis + pricing recommendation") | |
| with gr.Tabs(): | |
| with gr.Tab("Dashboard"): | |
| if ARTIFACTS_OK: | |
| gr.Dataframe(value=df_pricing, label="Pricing Decisions Table") | |
| else: | |
| gr.Markdown("No artifacts found yet.") | |
| with gr.Tab("Analyze a New Book"): | |
| title_input = gr.Textbox(label="Book Title") | |
| units_input = gr.Number(label="Avg Monthly Units Sold", value=100) | |
| reviews_input = gr.Textbox(label="Reviews (one per line)", lines=6) | |
| analyze_btn = gr.Button("Analyze") | |
| summary_output = gr.Textbox(label="Summary", lines=6) | |
| details_output = gr.Textbox(label="Details", lines=2) | |
| chart_output = gr.Plot(label="Chart") | |
| analyze_btn.click( | |
| fn=analyze_book, | |
| inputs=[title_input, reviews_input, units_input], | |
| outputs=[summary_output, details_output, chart_output], | |
| ) | |
| with gr.Tab("About"): | |
| gr.Markdown("AI for Big Data Management project app.") | |
| app.launch() |