farouk1 commited on
Commit
3af5f2a
·
verified ·
1 Parent(s): 609ea46

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -27
app.py CHANGED
@@ -1,44 +1,78 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
- # Using a powerful model for creative text generation
5
- MODEL = "google/flan-t5-large"
6
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
7
  model = AutoModelForSeq2SeqLM.from_pretrained(MODEL)
8
 
9
- def generate_story(topic, style_choice, lang_choice):
10
- # Language and style selection based on user input
 
 
 
 
 
 
 
 
 
11
  if lang_choice == "Arabic":
12
  if style_choice == "Blog Post (Descriptive)":
13
- prompt = f"اكتب مقالاً مفصلاً لمدونة باللغة العربية عن: {topic}. اجعل النغمة شخصية، ووصف الأماكن والأحداث بشكل جذاب."
14
  elif style_choice == "Social Media Post (Short & Catchy)":
15
- prompt = f"اكتب منشوراً قصيراً وجذاباً باللغة العربية عن: {topic}، مع إيموجي وهاشتاغات مقترحة."
16
- else: # Video Script (Storytelling)
17
- prompt = f"اكتب سيناريو فيديو (script) باللغة العربية عن: {topic}. ركز على سرد قصة عاطفية وجذابة، مع وصف للمشاهد."
18
- else: # English
19
  if style_choice == "Blog Post (Descriptive)":
20
- prompt = f"Write a detailed blog post in English about: {topic}. Make the tone personal, and describe the places and events attractively."
21
  elif style_choice == "Social Media Post (Short & Catchy)":
22
- prompt = f"Write a short and catchy social media post in English about: {topic}, with emojis and suggested hashtags."
23
- else: # Video Script (Storytelling)
24
- prompt = f"Write a video script (storytelling) in English about: {topic}. Focus on a personal and emotional story, with scene descriptions."
25
 
26
  try:
27
  inputs = tokenizer(prompt, return_tensors="pt")
28
- outs = model.generate(**inputs, max_length=500, num_beams=5, early_stopping=True)
29
- return tokenizer.decode(outs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
30
  except Exception as e:
31
- return f"An error occurred: {str(e)}. Please make sure the topic is clearly written."
32
-
33
- iface = gr.Interface(
34
- fn=generate_story,
35
- inputs=[
36
- gr.Textbox(label="Topic (e.g., 'My trip to Paris, seeing the Eiffel Tower and eating croissants')"),
37
- gr.Radio(["Blog Post (Descriptive)", "Social Media Post (Short & Catchy)", "Video Script (Storytelling)"], label="Style"),
38
- gr.Radio(["Arabic", "English"], label="Language")
39
- ],
40
- outputs=gr.Textbox(label="Result", lines=10)
41
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  if __name__ == "__main__":
44
- iface.launch()
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
+ # نموذج أقوى
5
+ MODEL = "google/flan-t5-xl"
6
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
7
  model = AutoModelForSeq2SeqLM.from_pretrained(MODEL)
8
 
9
+ # مولد النصوص
10
+ def generate_content(topic, style_choice, lang_choice, length_choice):
11
+ # اختيار الطول
12
+ if length_choice == "Short":
13
+ max_len = 200
14
+ elif length_choice == "Medium":
15
+ max_len = 400
16
+ else: # Long
17
+ max_len = 700
18
+
19
+ # بناء البرومبت
20
  if lang_choice == "Arabic":
21
  if style_choice == "Blog Post (Descriptive)":
22
+ prompt = f"اكتب مقالاً احترافياً بأسلوب شخصي عن: {topic}. ركز على التفاصيل والوصف الجذاب. اجعل النص منسقاً بفقرات."
23
  elif style_choice == "Social Media Post (Short & Catchy)":
24
+ prompt = f"اكتب منشوراً قصيراً وجذاباً عن: {topic}. أضف إيموجي مناسبة واقترح 3 هاشتاغات."
25
+ else: # Video Script
26
+ prompt = f"اكتب سيناريو فيديو احترافي عن: {topic}. اجعل الأسلوب قصصي وسردي مع تقسيمه إلى مشاهد."
27
+ else: # English
28
  if style_choice == "Blog Post (Descriptive)":
29
+ prompt = f"Write a professional blog post about: {topic}. Make it personal and descriptive, well-structured in paragraphs."
30
  elif style_choice == "Social Media Post (Short & Catchy)":
31
+ prompt = f"Write a short and catchy social media post about: {topic}, add emojis and suggest 3 hashtags."
32
+ else: # Video Script
33
+ prompt = f"Write a professional video script about: {topic}. Make it emotional, story-driven, and divided into scenes."
34
 
35
  try:
36
  inputs = tokenizer(prompt, return_tensors="pt")
37
+ outputs = model.generate(
38
+ **inputs,
39
+ max_length=max_len,
40
+ num_beams=5,
41
+ temperature=0.8,
42
+ early_stopping=True
43
+ )
44
+ content = tokenizer.decode(outputs[0], skip_special_tokens=True)
45
+
46
+ return content
47
  except Exception as e:
48
+ return f"⚠️ Error: {str(e)}"
49
+
50
+ # واجهة Gradio
51
+ with gr.Blocks(theme="default") as iface:
52
+ gr.Markdown("## ✨ AI Content Pack - Create Blogs, Posts & Scripts in Seconds")
53
+
54
+ with gr.Row():
55
+ topic = gr.Textbox(label="Topic / الموضوع", placeholder="مثال: رحلتي إلى باريس وتجربة برج إيفل...")
56
+
57
+ with gr.Row():
58
+ style_choice = gr.Radio(
59
+ ["Blog Post (Descriptive)", "Social Media Post (Short & Catchy)", "Video Script (Storytelling)"],
60
+ label="Style / نوع المحتوى"
61
+ )
62
+ lang_choice = gr.Radio(["Arabic", "English"], label="Language / اللغة")
63
+ length_choice = gr.Radio(["Short", "Medium", "Long"], label="Length / طول النص", value="Medium")
64
+
65
+ output = gr.Textbox(label="Generated Content", lines=20)
66
+ download_btn = gr.File(label="Download", file_types=[".txt"])
67
+
68
+ def save_file(content):
69
+ with open("generated.txt", "w", encoding="utf-8") as f:
70
+ f.write(content)
71
+ return "generated.txt"
72
+
73
+ btn = gr.Button("🚀 Generate Content")
74
+ btn.click(fn=generate_content, inputs=[topic, style_choice, lang_choice, length_choice], outputs=output)
75
+ output.change(fn=save_file, inputs=output, outputs=download_btn)
76
 
77
  if __name__ == "__main__":
78
+ iface.launch()