Talip7 commited on
Commit
c8befe2
·
verified ·
1 Parent(s): f476279

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import gradio as gr
4
+ import pdfplumber
5
+ from openai import OpenAI
6
+
7
+ client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
8
+
9
+ LANGUAGES = ["English", "Turkish", "Arabic", "French", "German", "Spanish"]
10
+
11
+ TRANSLATIONS = {
12
+ "English": {
13
+ "summary_lang": "🗣️ Summary Language",
14
+ "char_limit": "🔢 Character Limit (approximate)",
15
+ "text_input": "📥 Paste Text Here",
16
+ "text_placeholder": "Or paste a document here...",
17
+ "pdf_upload": "📄 Or Upload a PDF File",
18
+ "output": "🧠 Output (Summary, Title, Keywords)",
19
+ "run_button": "Summarize"
20
+ },
21
+ "Turkish": {
22
+ "summary_lang": "🗣️ Özetleme Dili",
23
+ "char_limit": "🔢 Karakter Sınırı (yaklaşık)",
24
+ "text_input": "📥 Metni Buraya Yapıştırın",
25
+ "text_placeholder": "Ya da bir belge yapıştırın...",
26
+ "pdf_upload": "📄 Ya da bir PDF Yükleyin",
27
+ "output": "🧠 Çıktı (Özet, Başlık, Anahtar Kelimeler)",
28
+ "run_button": "Özetle"
29
+ }
30
+ # Diğer diller buraya eklenebilir...
31
+ }
32
+
33
+ def extract_text_from_pdf(file):
34
+ try:
35
+ with pdfplumber.open(file.name) as pdf:
36
+ text = ""
37
+ for page in pdf.pages:
38
+ page_text = page.extract_text()
39
+ if page_text:
40
+ text += page_text + "\n"
41
+ if not text.strip():
42
+ return None, "The PDF appears to contain no extractable text."
43
+ return text.strip(), None
44
+ except Exception as e:
45
+ return None, f"PDF reading error: {str(e)}"
46
+
47
+ def translate_text(text, target_lang):
48
+ try:
49
+ prompt = f"Translate the following text to {target_lang}:\n\n{text}"
50
+ response = client.chat.completions.create(
51
+ model="gpt-3.5-turbo",
52
+ messages=[
53
+ {"role": "system", "content": "You are a helpful translator."},
54
+ {"role": "user", "content": prompt}
55
+ ],
56
+ temperature=0.3,
57
+ max_tokens=3000
58
+ )
59
+ return response.choices[0].message.content
60
+ except Exception:
61
+ return text
62
+
63
+ def analyze_text(text, summary_lang, char_limit):
64
+ translated_text = translate_text(text, summary_lang)
65
+ prompt = f"""You are a helpful assistant.
66
+
67
+ Please provide the following in {summary_lang}:
68
+ 1. A clear and concise summary (limited to approximately {char_limit} characters).
69
+ 2. A suitable title.
70
+ 3. 5 relevant keywords.
71
+
72
+ Document:
73
+ {translated_text[:3000]}"""
74
+ try:
75
+ response = client.chat.completions.create(
76
+ model="gpt-3.5-turbo",
77
+ messages=[
78
+ {"role": "system", "content": "You are a helpful assistant."},
79
+ {"role": "user", "content": prompt}
80
+ ],
81
+ temperature=0.7,
82
+ max_tokens=400
83
+ )
84
+ return response.choices[0].message.content
85
+ except Exception as e:
86
+ return f"❌ OpenAI Error: {str(e)}"
87
+
88
+ def analyze_input(summary_lang, char_limit, text_input, pdf_file):
89
+ text = ""
90
+ error = None
91
+ if pdf_file:
92
+ text, error = extract_text_from_pdf(pdf_file)
93
+ elif text_input and text_input.strip():
94
+ text = text_input.strip()
95
+
96
+ if error:
97
+ return f"⚠️ PDF Error: {error}"
98
+ if not text:
99
+ return "⚠️ No text provided or extracted."
100
+
101
+ return analyze_text(text, summary_lang, char_limit)
102
+
103
+ def interface_selector(interface_lang):
104
+ t = TRANSLATIONS.get(interface_lang, TRANSLATIONS["English"])
105
+ return (
106
+ gr.update(visible=True),
107
+ gr.update(label=t["summary_lang"]),
108
+ gr.update(label=t["char_limit"]),
109
+ gr.update(label=t["text_input"], placeholder=t["text_placeholder"]),
110
+ gr.update(label=t["pdf_upload"]),
111
+ gr.update(label=t["output"]),
112
+ gr.update(value=t["run_button"])
113
+ )
114
+
115
+ with gr.Blocks() as demo:
116
+ gr.Markdown("## 🌐 Select Interface Language")
117
+
118
+ with gr.Accordion("📘 View README / Usage Guide", open=False):
119
+ gr.Markdown("""This application allows you to upload a PDF or paste text, select your preferred summary language, and receive:
120
+
121
+ - A clear summary ✂️
122
+ - An auto-generated title 🏷️
123
+ - 5 relevant keywords 🔑
124
+
125
+ If the content language and summary language differ, the app will auto-translate before summarizing 🌐
126
+
127
+ Powered by OpenAI GPT-3.5 and Gradio.""")
128
+
129
+ lang_select = gr.Dropdown(label="Interface Language", choices=LANGUAGES, value="English")
130
+ next_btn = gr.Button("Continue")
131
+
132
+ with gr.Column(visible=False) as summary_section:
133
+ summary_lang = gr.Dropdown(choices=LANGUAGES, value="English")
134
+ char_limit = gr.Textbox(value="300")
135
+ text_input = gr.Textbox(lines=10)
136
+ pdf_file = gr.File()
137
+ output = gr.Textbox()
138
+ run_btn = gr.Button()
139
+
140
+ next_btn.click(fn=interface_selector, inputs=[lang_select], outputs=[
141
+ summary_section,
142
+ summary_lang,
143
+ char_limit,
144
+ text_input,
145
+ pdf_file,
146
+ output,
147
+ run_btn
148
+ ])
149
+
150
+ run_btn.click(fn=analyze_input, inputs=[summary_lang, char_limit, text_input, pdf_file], outputs=output)
151
+
152
+ demo.launch()