Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,130 +1,120 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
import logging
|
| 5 |
-
import json
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
logging.basicConfig(level=logging.INFO
|
| 9 |
-
logger = logging.getLogger(
|
| 10 |
|
| 11 |
-
# ---------------- MODEL CLASS ----------------
|
| 12 |
class FrenchTitleGenerator:
|
| 13 |
-
def __init__(self
|
| 14 |
-
logger.info(f"Loading model: {model_name}")
|
| 15 |
-
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
-
logger.error(f"Error loading model: {e}")
|
| 22 |
-
|
| 23 |
|
| 24 |
-
def
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
city = str(city).strip()
|
| 29 |
-
original_title = str(original_title).strip()
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
temperature=float(temperature),
|
| 45 |
-
num_beams=3,
|
| 46 |
-
early_stopping=True
|
| 47 |
-
)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
final_result = result
|
| 63 |
-
|
| 64 |
-
return {
|
| 65 |
-
"success": True,
|
| 66 |
-
"french_title": final_result,
|
| 67 |
-
"input_used": {
|
| 68 |
-
"year": year,
|
| 69 |
-
"city": city,
|
| 70 |
-
"original_title": original_title,
|
| 71 |
-
"temperature": temperature,
|
| 72 |
-
"max_length": max_length
|
| 73 |
-
}
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
except Exception as e:
|
| 77 |
-
logger.error(f"Error during generation: {e}")
|
| 78 |
-
return {"error": f"Error generating title: {str(e)}"}
|
| 79 |
-
|
| 80 |
-
# ---------------- INIT MODEL ----------------
|
| 81 |
-
try:
|
| 82 |
-
generator = FrenchTitleGenerator()
|
| 83 |
-
model_loaded = True
|
| 84 |
-
logger.info("Application started successfully")
|
| 85 |
-
except Exception as e:
|
| 86 |
-
logger.error(f"Failed to initialize model: {e}")
|
| 87 |
-
generator = None
|
| 88 |
-
model_loaded = False
|
| 89 |
-
|
| 90 |
-
# ---------------- GRADIO APP ----------------
|
| 91 |
-
def generate_title(year, city, original_title, temperature=0.8, max_length=50):
|
| 92 |
-
if not model_loaded:
|
| 93 |
-
return "Error: Model failed to load. Please check the logs."
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# Create interface
|
| 103 |
-
iface = gr.Interface(
|
| 104 |
-
fn=generate_title,
|
| 105 |
-
inputs=[
|
| 106 |
-
gr.Textbox(label="Year", placeholder="e.g. 2025", value=""),
|
| 107 |
-
gr.Textbox(label="City", placeholder="e.g. Paris", value=""),
|
| 108 |
-
gr.Textbox(
|
| 109 |
-
label="Event Description",
|
| 110 |
-
placeholder="e.g. Jazz Festival in the City Park",
|
| 111 |
-
lines=2
|
| 112 |
-
),
|
| 113 |
-
gr.Slider(0.1, 1.5, value=0.8, step=0.1, label="Creativity (Temperature)"),
|
| 114 |
-
gr.Slider(20, 100, value=50, step=5, label="Max Title Length")
|
| 115 |
-
],
|
| 116 |
-
outputs=gr.Textbox(
|
| 117 |
-
label="Generated French Title"
|
| 118 |
-
),
|
| 119 |
-
title="🇫🇷 French Event Title Generator",
|
| 120 |
-
description="Generate elegant French titles for your events based on year, city, and description.",
|
| 121 |
-
allow_flagging="never",
|
| 122 |
-
api_name="predict"
|
| 123 |
-
)
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|
| 126 |
-
|
| 127 |
-
server_name="0.0.0.0",
|
| 128 |
-
server_port=7860,
|
| 129 |
-
share=False
|
| 130 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import pandas as pd
|
| 4 |
import logging
|
|
|
|
| 5 |
|
| 6 |
+
# Set up logging
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
|
|
|
|
| 10 |
class FrenchTitleGenerator:
|
| 11 |
+
def __init__(self):
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
+
# Load your fine-tuned model from Hugging Face
|
| 14 |
+
# Replace with your actual model name after training
|
| 15 |
+
self.generator = pipeline(
|
| 16 |
+
"translation",
|
| 17 |
+
model="sufya12/french-title-model", # Change this to your model
|
| 18 |
+
tokenizer="sufya12/french-title-model"
|
| 19 |
+
)
|
| 20 |
+
logger.info("✅ Model loaded successfully from Hugging Face")
|
| 21 |
except Exception as e:
|
| 22 |
+
logger.error(f"❌ Error loading model: {e}")
|
| 23 |
+
self.generator = None
|
| 24 |
|
| 25 |
+
def generate_title(self, year, city, event_description, temperature=0.8, max_length=60):
|
| 26 |
+
if not self.generator:
|
| 27 |
+
return "Error: Model not loaded. Please check the logs."
|
| 28 |
+
|
| 29 |
try:
|
| 30 |
+
# Create input prompt
|
| 31 |
+
input_text = f"Generate French title: Year: {year} City: {city} Event: {event_description}"
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Generate title
|
| 34 |
+
result = self.generator(
|
| 35 |
+
input_text,
|
| 36 |
+
max_length=max_length,
|
| 37 |
+
num_beams=3,
|
| 38 |
+
temperature=temperature,
|
| 39 |
+
do_sample=True,
|
| 40 |
+
early_stopping=True
|
| 41 |
+
)
|
| 42 |
|
| 43 |
+
return result[0]['translation_text']
|
| 44 |
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error generating title: {str(e)}"
|
| 47 |
|
| 48 |
+
# Initialize the generator
|
| 49 |
+
title_generator = FrenchTitleGenerator()
|
| 50 |
+
|
| 51 |
+
# Gradio interface
|
| 52 |
+
def generate_french_title(year, city, event_description, temperature, max_length):
|
| 53 |
+
return title_generator.generate_title(year, city, event_description, temperature, max_length)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Create the interface
|
| 56 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 57 |
+
gr.Markdown("# 🇫🇷 French Event Title Generator")
|
| 58 |
+
gr.Markdown("Generate professional French event titles using AI!")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
year_input = gr.Textbox(
|
| 63 |
+
label="Year",
|
| 64 |
+
placeholder="e.g., 2025",
|
| 65 |
+
value="2025"
|
| 66 |
+
)
|
| 67 |
+
city_input = gr.Textbox(
|
| 68 |
+
label="City",
|
| 69 |
+
placeholder="e.g., Paris",
|
| 70 |
+
value="Paris"
|
| 71 |
+
)
|
| 72 |
+
event_input = gr.Textbox(
|
| 73 |
+
label="Event Description",
|
| 74 |
+
placeholder="Describe your event in English...",
|
| 75 |
+
lines=3
|
| 76 |
+
)
|
| 77 |
|
| 78 |
+
with gr.Row():
|
| 79 |
+
temperature_slider = gr.Slider(
|
| 80 |
+
minimum=0.1,
|
| 81 |
+
maximum=1.5,
|
| 82 |
+
value=0.8,
|
| 83 |
+
step=0.1,
|
| 84 |
+
label="Creativity Level"
|
| 85 |
+
)
|
| 86 |
+
max_length_slider = gr.Slider(
|
| 87 |
+
minimum=20,
|
| 88 |
+
maximum=100,
|
| 89 |
+
value=60,
|
| 90 |
+
step=5,
|
| 91 |
+
label="Title Length"
|
| 92 |
+
)
|
| 93 |
|
| 94 |
+
generate_btn = gr.Button("Generate Title 🚀", variant="primary")
|
| 95 |
+
|
| 96 |
+
with gr.Column():
|
| 97 |
+
output_text = gr.Textbox(
|
| 98 |
+
label="Generated French Title",
|
| 99 |
+
lines=4,
|
| 100 |
+
show_copy_button=True
|
| 101 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# Examples
|
| 104 |
+
gr.Examples(
|
| 105 |
+
examples=[
|
| 106 |
+
["2025", "Paris", "Summer Jazz Festival in the Park"],
|
| 107 |
+
["2024", "Lyon", "International Food and Wine Exhibition"],
|
| 108 |
+
["2025", "Nice", "Beach Music Concert and Party"]
|
| 109 |
+
],
|
| 110 |
+
inputs=[year_input, city_input, event_input]
|
| 111 |
+
)
|
| 112 |
|
| 113 |
+
generate_btn.click(
|
| 114 |
+
fn=generate_french_title,
|
| 115 |
+
inputs=[year_input, city_input, event_input, temperature_slider, max_length_slider],
|
| 116 |
+
outputs=output_text
|
| 117 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|