Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import MarianMTModel, MarianTokenizer
|
| 4 |
import logging
|
| 5 |
-
import
|
| 6 |
|
| 7 |
# ---------------- LOGGING ----------------
|
| 8 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
@@ -29,7 +29,7 @@ class FrenchTitleGenerator:
|
|
| 29 |
original_title = str(original_title).strip()
|
| 30 |
|
| 31 |
if not original_title:
|
| 32 |
-
return "Please enter an event description."
|
| 33 |
|
| 34 |
input_text = f"Translate to French: {original_title}"
|
| 35 |
|
|
@@ -53,19 +53,31 @@ class FrenchTitleGenerator:
|
|
| 53 |
|
| 54 |
# Add context if needed
|
| 55 |
if year and city:
|
| 56 |
-
|
| 57 |
elif city:
|
| 58 |
-
|
| 59 |
elif year:
|
| 60 |
-
|
| 61 |
else:
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
logger.error(f"Error during generation: {e}")
|
| 66 |
-
return f"Error generating title: {str(e)}"
|
| 67 |
|
| 68 |
-
# ---------------- INIT MODEL
|
| 69 |
try:
|
| 70 |
generator = FrenchTitleGenerator()
|
| 71 |
model_loaded = True
|
|
@@ -80,12 +92,14 @@ def generate_title(year, city, original_title, temperature=0.8, max_length=50):
|
|
| 80 |
if not model_loaded:
|
| 81 |
return "Error: Model failed to load. Please check the logs."
|
| 82 |
|
| 83 |
-
|
| 84 |
-
return "Please enter an event description."
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
# Create
|
| 89 |
iface = gr.Interface(
|
| 90 |
fn=generate_title,
|
| 91 |
inputs=[
|
|
@@ -104,9 +118,13 @@ iface = gr.Interface(
|
|
| 104 |
),
|
| 105 |
title="🇫🇷 French Event Title Generator",
|
| 106 |
description="Generate elegant French titles for your events based on year, city, and description.",
|
| 107 |
-
allow_flagging="never"
|
|
|
|
| 108 |
)
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import MarianMTModel, MarianTokenizer
|
| 4 |
import logging
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
# ---------------- LOGGING ----------------
|
| 8 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
|
| 29 |
original_title = str(original_title).strip()
|
| 30 |
|
| 31 |
if not original_title:
|
| 32 |
+
return {"error": "Please enter an event description."}
|
| 33 |
|
| 34 |
input_text = f"Translate to French: {original_title}"
|
| 35 |
|
|
|
|
| 53 |
|
| 54 |
# Add context if needed
|
| 55 |
if year and city:
|
| 56 |
+
final_result = f"{result} - {city} {year}"
|
| 57 |
elif city:
|
| 58 |
+
final_result = f"{result} - {city}"
|
| 59 |
elif year:
|
| 60 |
+
final_result = f"{result} - {year}"
|
| 61 |
else:
|
| 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
|
|
|
|
| 92 |
if not model_loaded:
|
| 93 |
return "Error: Model failed to load. Please check the logs."
|
| 94 |
|
| 95 |
+
result = generator.generate(year, city, original_title, max_length, temperature)
|
|
|
|
| 96 |
|
| 97 |
+
if "error" in result:
|
| 98 |
+
return result["error"]
|
| 99 |
+
else:
|
| 100 |
+
return result["french_title"]
|
| 101 |
|
| 102 |
+
# Create interface
|
| 103 |
iface = gr.Interface(
|
| 104 |
fn=generate_title,
|
| 105 |
inputs=[
|
|
|
|
| 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 |
+
iface.launch(
|
| 127 |
+
server_name="0.0.0.0",
|
| 128 |
+
server_port=7860,
|
| 129 |
+
share=False
|
| 130 |
+
)
|