Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,18 @@
|
|
| 1 |
# app.py
|
| 2 |
-
#
|
| 3 |
-
# Bu kod
|
| 4 |
-
#
|
| 5 |
-
# şeklinde metin oluşturma akışını gösterir. Minimum 4000, maksimum 10000 kelime
|
| 6 |
-
# üretilmesi hedeflenir. Kod, Gradio ile görsel bir arayüz sunar.
|
| 7 |
#
|
| 8 |
-
#
|
| 9 |
-
#
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
-
# Bu proje tamamen insan emeğiyle yazılmıştır, geliştirici tarafından tasarlanmıştır.
|
| 13 |
-
# --------------------------------------------------------------------------------
|
| 14 |
|
| 15 |
import os
|
| 16 |
import re
|
| 17 |
import gradio as gr
|
| 18 |
|
| 19 |
-
#
|
| 20 |
try:
|
| 21 |
from openai import OpenAI
|
| 22 |
import tiktoken
|
|
@@ -25,131 +21,164 @@ try:
|
|
| 25 |
except ImportError:
|
| 26 |
raise ImportError("Lütfen 'openai', 'tiktoken', 'gradio', 'PyPDF2', 'python-docx' paketlerini kurun.")
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
# GPT-4o-mini modelini kullanacağımız API istemcisi:
|
| 30 |
client = OpenAI(api_key="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")
|
| 31 |
|
| 32 |
def call_openai_chat(messages, max_tokens=10000, temperature=0.8):
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
-
- max_tokens=10000
|
| 36 |
-
- temperature=0.8
|
| 37 |
"""
|
| 38 |
response = client.chat.completions.create(
|
| 39 |
model="gpt-4o-mini",
|
| 40 |
messages=messages,
|
| 41 |
max_tokens=max_tokens,
|
| 42 |
temperature=temperature,
|
| 43 |
-
stop=None
|
| 44 |
)
|
| 45 |
return response.choices[0].message.content
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
def
|
| 49 |
"""
|
| 50 |
-
Chunk #1 -> Heading 1
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
"""
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
Input text:
|
| 59 |
{input_text}
|
| 60 |
"""
|
| 61 |
-
|
| 62 |
-
{"role": "system", "content": "You are
|
| 63 |
-
{"role": "user", "content":
|
| 64 |
]
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
"""
|
| 69 |
-
|
| 70 |
-
Ilk parçayı genişleterek final haline getirir (örn. 2000+ words).
|
| 71 |
-
"""
|
| 72 |
-
user_content = f"""
|
| 73 |
-
Below is the partial text for heading 1:
|
| 74 |
{h1_part1_text}
|
| 75 |
|
| 76 |
-
Now finalize heading 1
|
| 77 |
-
Ensure
|
| 78 |
-
Return
|
| 79 |
"""
|
| 80 |
-
|
| 81 |
{"role": "system", "content": "You are finalizing heading #1."},
|
| 82 |
-
{"role": "user", "content":
|
| 83 |
]
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
"""
|
| 88 |
-
Chunk #3
|
| 89 |
-
|
|
|
|
| 90 |
"""
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
Context:
|
| 97 |
-
{
|
| 98 |
"""
|
| 99 |
-
|
| 100 |
-
{"role": "system", "content": "You are
|
| 101 |
-
{"role": "user", "content":
|
| 102 |
]
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
|
|
|
| 106 |
"""
|
| 107 |
-
Chunk #5
|
| 108 |
-
|
| 109 |
"""
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
{heading1_text}
|
| 115 |
|
| 116 |
-
[
|
| 117 |
{heading2_text}
|
| 118 |
|
| 119 |
-
[
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
If
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
Return the final text with headings 1,2,3,4 merged.
|
| 128 |
-
No separate block, but unify expansions or edits.
|
| 129 |
|
| 130 |
-
|
| 131 |
{input_text}
|
| 132 |
"""
|
| 133 |
-
|
| 134 |
-
{"role": "system", "content": "You are
|
| 135 |
-
{"role": "user", "content":
|
| 136 |
]
|
| 137 |
-
|
|
|
|
| 138 |
|
| 139 |
-
#
|
| 140 |
def read_pdf(file_path: str) -> str:
|
| 141 |
-
"""Reads text from a PDF file (simple approach)."""
|
| 142 |
text = ""
|
| 143 |
with open(file_path, "rb") as f:
|
| 144 |
-
|
| 145 |
-
for page in
|
| 146 |
-
|
| 147 |
-
if
|
| 148 |
-
text +=
|
| 149 |
return text
|
| 150 |
|
| 151 |
def read_docx(file_path: str) -> str:
|
| 152 |
-
"""Reads text from a DOCX file."""
|
| 153 |
doc = Document(file_path)
|
| 154 |
paragraphs = []
|
| 155 |
for para in doc.paragraphs:
|
|
@@ -157,117 +186,102 @@ def read_docx(file_path: str) -> str:
|
|
| 157 |
return "\n".join(paragraphs)
|
| 158 |
|
| 159 |
def read_txt(file_path: str) -> str:
|
| 160 |
-
"""Reads text from a .txt file."""
|
| 161 |
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 162 |
return f.read()
|
| 163 |
|
| 164 |
-
#
|
| 165 |
-
def
|
| 166 |
"""
|
| 167 |
-
|
| 168 |
-
txt_input: text (str)
|
| 169 |
-
file_obj: gradio üzerinden gelen file nesnesi
|
| 170 |
"""
|
| 171 |
-
# Eğer dosya yüklenmişse
|
| 172 |
if file_obj is not None:
|
| 173 |
-
# file_obj
|
| 174 |
-
|
| 175 |
-
content = file_obj.read()
|
| 176 |
-
|
| 177 |
-
with open(file_name, "wb") as tmp:
|
| 178 |
tmp.write(content)
|
| 179 |
|
| 180 |
-
ext =
|
| 181 |
if ext == "pdf":
|
| 182 |
-
return read_pdf(
|
| 183 |
elif ext == "docx":
|
| 184 |
-
return read_docx(
|
| 185 |
elif ext == "txt":
|
| 186 |
-
return read_txt(
|
| 187 |
else:
|
| 188 |
# fallback decode
|
| 189 |
return content.decode("utf-8", errors="ignore")
|
| 190 |
else:
|
| 191 |
-
|
| 192 |
-
return txt_input.strip()
|
| 193 |
|
| 194 |
-
def
|
| 195 |
"""
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
5) Heading4 + expansions => final text (chunk #5)
|
| 201 |
"""
|
| 202 |
-
#
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
#
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
#
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
#
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
wcount = len(plain_text.split())
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
return final_html, info
|
| 226 |
|
| 227 |
-
def gradio_interface(txt_input, file_upload):
|
| 228 |
-
# Tek fonksiyon, hem input hem output
|
| 229 |
-
read_content = process_input_text_or_file(txt_input, file_upload)
|
| 230 |
-
if not read_content:
|
| 231 |
-
return "⚠️ Please provide text or file input.", ""
|
| 232 |
-
# 5-chunk workflow
|
| 233 |
-
final_html, info = generate_5_chunks(read_content)
|
| 234 |
-
return final_html, info
|
| 235 |
|
| 236 |
-
#
|
| 237 |
-
def
|
| 238 |
-
# "inputs" parametresine, txt ve file girişi ekleyeceğiz
|
| 239 |
text_input = gr.Textbox(
|
| 240 |
lines=5,
|
| 241 |
label="Text Input (Optional)",
|
| 242 |
-
placeholder="
|
| 243 |
)
|
| 244 |
file_input = gr.File(
|
| 245 |
-
label="Upload
|
| 246 |
-
file_types=[".pdf",
|
| 247 |
)
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
info_label = gr.Label(label="Process Info (Word Count etc.)")
|
| 251 |
|
| 252 |
-
# Arayüz
|
| 253 |
demo = gr.Interface(
|
| 254 |
-
fn=
|
| 255 |
inputs=[text_input, file_input],
|
| 256 |
outputs=[output_html, info_label],
|
| 257 |
-
title="
|
| 258 |
description=(
|
| 259 |
-
"
|
| 260 |
-
"
|
| 261 |
-
"
|
| 262 |
-
"
|
| 263 |
-
"
|
| 264 |
)
|
| 265 |
)
|
| 266 |
return demo
|
| 267 |
|
| 268 |
-
# app.py main
|
| 269 |
if __name__ == "__main__":
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
# genelde local (127.0.0.1:7860) host
|
| 273 |
-
demo_app.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# -------------------------------------------------------------------------
|
| 3 |
+
# NOT: Bu kod tamamen insan (developer) tarafından yazılmıştır, GPT veya
|
| 4 |
+
# başka bir yapay zeka tarafından üretilmemiştir. Eğitim amaçlı paylaşılmaktadır.
|
|
|
|
|
|
|
| 5 |
#
|
| 6 |
+
# Model: gpt-4o-mini
|
| 7 |
+
# Min. 4000 kelime, Max. 10000 kelime
|
| 8 |
+
# 3 ayrı API çağrısı, her çağrıda 2 chunk -> 6 chunk toplam
|
| 9 |
+
# -------------------------------------------------------------------------
|
|
|
|
|
|
|
| 10 |
|
| 11 |
import os
|
| 12 |
import re
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
+
# Gerekli kütüphaneler
|
| 16 |
try:
|
| 17 |
from openai import OpenAI
|
| 18 |
import tiktoken
|
|
|
|
| 21 |
except ImportError:
|
| 22 |
raise ImportError("Lütfen 'openai', 'tiktoken', 'gradio', 'PyPDF2', 'python-docx' paketlerini kurun.")
|
| 23 |
|
| 24 |
+
# ============== 1) OPENAI API İstemcisi ================
|
|
|
|
| 25 |
client = OpenAI(api_key="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")
|
| 26 |
|
| 27 |
def call_openai_chat(messages, max_tokens=10000, temperature=0.8):
|
| 28 |
"""
|
| 29 |
+
Tek seferde gpt-4o-mini modeline istek atar.
|
| 30 |
+
- max_tokens=10000 => uzun çıktı
|
| 31 |
+
- temperature=0.8 => daha yaratıcı/uzun
|
| 32 |
"""
|
| 33 |
response = client.chat.completions.create(
|
| 34 |
model="gpt-4o-mini",
|
| 35 |
messages=messages,
|
| 36 |
max_tokens=max_tokens,
|
| 37 |
temperature=temperature,
|
| 38 |
+
stop=None
|
| 39 |
)
|
| 40 |
return response.choices[0].message.content
|
| 41 |
|
| 42 |
+
# ============== 2) CHUNK #1 ve #2 => (API Çağrısı #1) ================
|
| 43 |
+
def heading1_part1_api_call(input_text):
|
| 44 |
"""
|
| 45 |
+
Chunk #1 -> Heading 1 (Part 1)
|
| 46 |
+
Chunk #2 -> Heading 1 (Part 2) => finalize
|
| 47 |
+
İki chunk'ı tek fonksiyon içinde ardışık olarak çalıştıran 1 API call.
|
| 48 |
+
Aslında 2 istek yapar ama mantıkta tek 'block' diyebiliriz.
|
| 49 |
"""
|
| 50 |
+
# Chunk #1
|
| 51 |
+
user_prompt_1 = f"""
|
| 52 |
+
We have some input. We want 'Heading 1: Introductory overview of input' in 2 parts.
|
| 53 |
+
Now produce PART 1 of heading 1 (~1000+ words). Return partial text, do NOT finalize.
|
| 54 |
+
Input:
|
|
|
|
| 55 |
{input_text}
|
| 56 |
"""
|
| 57 |
+
messages_1 = [
|
| 58 |
+
{"role": "system", "content": "You are an AI assistant creating heading 1 (part 1)."},
|
| 59 |
+
{"role": "user", "content": user_prompt_1}
|
| 60 |
]
|
| 61 |
+
h1_part1_text = call_openai_chat(messages_1)
|
| 62 |
|
| 63 |
+
# Chunk #2
|
| 64 |
+
user_prompt_2 = f"""
|
| 65 |
+
We have partial heading 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
{h1_part1_text}
|
| 67 |
|
| 68 |
+
Now finalize heading 1 (PART 2).
|
| 69 |
+
Ensure total heading1 is at least 2000 words.
|
| 70 |
+
Add expansions or clarifications. Return the final heading1 text only.
|
| 71 |
"""
|
| 72 |
+
messages_2 = [
|
| 73 |
{"role": "system", "content": "You are finalizing heading #1."},
|
| 74 |
+
{"role": "user", "content": user_prompt_2}
|
| 75 |
]
|
| 76 |
+
h1_final_text = call_openai_chat(messages_2)
|
| 77 |
|
| 78 |
+
return h1_final_text
|
| 79 |
+
|
| 80 |
+
# ============== 3) CHUNK #3 ve #4 => (API Çağrısı #2) ================
|
| 81 |
+
def heading2_3_api_call(heading1_text):
|
| 82 |
"""
|
| 83 |
+
Chunk #3 => Heading 2
|
| 84 |
+
Chunk #4 => Heading 3
|
| 85 |
+
Tek fonksiyonda ardışık 2 istek => 2 chunk
|
| 86 |
"""
|
| 87 |
+
# Chunk #3 => Heading2
|
| 88 |
+
prompt_h2 = f"""
|
| 89 |
+
We have heading1 for context. Now produce 'Heading 2: Detailed explanation of common risks.'
|
| 90 |
+
Aim ~1000+ words if possible. Return final heading2 text only.
|
| 91 |
|
| 92 |
Context:
|
| 93 |
+
{heading1_text}
|
| 94 |
"""
|
| 95 |
+
messages_h2 = [
|
| 96 |
+
{"role": "system", "content": "You are an AI assistant creating heading2."},
|
| 97 |
+
{"role": "user", "content": prompt_h2}
|
| 98 |
]
|
| 99 |
+
heading2_text = call_openai_chat(messages_h2)
|
| 100 |
+
|
| 101 |
+
# Chunk #4 => Heading3
|
| 102 |
+
prompt_h3 = f"""
|
| 103 |
+
We have heading1 for context. Now produce 'Heading 3: Practical examples and solutions.'
|
| 104 |
+
Aim ~1000+ words if possible. Return final heading3 text only.
|
| 105 |
+
|
| 106 |
+
Context:
|
| 107 |
+
{heading1_text}
|
| 108 |
+
"""
|
| 109 |
+
messages_h3 = [
|
| 110 |
+
{"role": "system", "content": "You are an AI assistant creating heading3."},
|
| 111 |
+
{"role": "user", "content": prompt_h3}
|
| 112 |
+
]
|
| 113 |
+
heading3_text = call_openai_chat(messages_h3)
|
| 114 |
+
|
| 115 |
+
return heading2_text, heading3_text
|
| 116 |
|
| 117 |
+
# ============== 4) CHUNK #5 ve #6 => (API Çağrısı #3) ================
|
| 118 |
+
def heading4_expansion_api_call(heading1_text, heading2_text, heading3_text, input_text):
|
| 119 |
"""
|
| 120 |
+
Chunk #5 => Heading4
|
| 121 |
+
Chunk #6 => expansions if <4000 words or shorten if >10000 words
|
| 122 |
"""
|
| 123 |
+
# Chunk #5 => heading4
|
| 124 |
+
prompt_h4 = f"""
|
| 125 |
+
We have heading1,2,3. Now produce 'Heading 4: Summary and next steps for students.'
|
| 126 |
+
At least 1000 words if possible. Return heading4 text only.
|
| 127 |
+
Context:
|
| 128 |
+
Heading1 = {heading1_text[:2000]}...
|
| 129 |
+
Heading2 = ...
|
| 130 |
+
Heading3 = ...
|
| 131 |
+
"""
|
| 132 |
+
messages_h4 = [
|
| 133 |
+
{"role": "system", "content": "You are an AI assistant creating heading4."},
|
| 134 |
+
{"role": "user", "content": prompt_h4}
|
| 135 |
+
]
|
| 136 |
+
heading4_text = call_openai_chat(messages_h4)
|
| 137 |
|
| 138 |
+
# Chunk #6 => expansions or shorten
|
| 139 |
+
prompt_expand = f"""
|
| 140 |
+
We have 4 headings:
|
| 141 |
+
|
| 142 |
+
[Heading1]
|
| 143 |
{heading1_text}
|
| 144 |
|
| 145 |
+
[Heading2]
|
| 146 |
{heading2_text}
|
| 147 |
|
| 148 |
+
[Heading3]
|
| 149 |
+
{heading3_text}
|
| 150 |
+
|
| 151 |
+
[Heading4]
|
| 152 |
+
{heading4_text}
|
| 153 |
|
| 154 |
+
Now combine them into ONE final text.
|
| 155 |
+
If total < 4000 words => expand.
|
| 156 |
+
If > 10000 words => shorten.
|
| 157 |
+
Return final text only, merged.
|
| 158 |
+
Add expansions to any heading if short, or remove details if too long.
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
Original input:
|
| 161 |
{input_text}
|
| 162 |
"""
|
| 163 |
+
messages_expand = [
|
| 164 |
+
{"role": "system", "content": "You are an AI assistant ensuring total word count 4000-10000."},
|
| 165 |
+
{"role": "user", "content": prompt_expand}
|
| 166 |
]
|
| 167 |
+
final_text = call_openai_chat(messages_expand)
|
| 168 |
+
return final_text
|
| 169 |
|
| 170 |
+
# ============== Dosya Okuma Fonksiyonları ================
|
| 171 |
def read_pdf(file_path: str) -> str:
|
|
|
|
| 172 |
text = ""
|
| 173 |
with open(file_path, "rb") as f:
|
| 174 |
+
pdf = PdfReader(f)
|
| 175 |
+
for page in pdf.pages:
|
| 176 |
+
txt = page.extract_text()
|
| 177 |
+
if txt:
|
| 178 |
+
text += txt
|
| 179 |
return text
|
| 180 |
|
| 181 |
def read_docx(file_path: str) -> str:
|
|
|
|
| 182 |
doc = Document(file_path)
|
| 183 |
paragraphs = []
|
| 184 |
for para in doc.paragraphs:
|
|
|
|
| 186 |
return "\n".join(paragraphs)
|
| 187 |
|
| 188 |
def read_txt(file_path: str) -> str:
|
|
|
|
| 189 |
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 190 |
return f.read()
|
| 191 |
|
| 192 |
+
# ============== Gradio Fonksiyon ================
|
| 193 |
+
def process_input_text(file_obj, txt):
|
| 194 |
"""
|
| 195 |
+
Dosya yüklenmişse okur, yoksa metin kutusunu alır
|
|
|
|
|
|
|
| 196 |
"""
|
|
|
|
| 197 |
if file_obj is not None:
|
| 198 |
+
# file_obj => gradio üzerinden
|
| 199 |
+
filename = file_obj.name
|
| 200 |
+
content = file_obj.read()
|
| 201 |
+
with open(filename, "wb") as tmp:
|
|
|
|
| 202 |
tmp.write(content)
|
| 203 |
|
| 204 |
+
ext = filename.lower().split('.')[-1]
|
| 205 |
if ext == "pdf":
|
| 206 |
+
return read_pdf(filename)
|
| 207 |
elif ext == "docx":
|
| 208 |
+
return read_docx(filename)
|
| 209 |
elif ext == "txt":
|
| 210 |
+
return read_txt(filename)
|
| 211 |
else:
|
| 212 |
# fallback decode
|
| 213 |
return content.decode("utf-8", errors="ignore")
|
| 214 |
else:
|
| 215 |
+
return txt.strip()
|
|
|
|
| 216 |
|
| 217 |
+
def main_pipeline(input_text):
|
| 218 |
"""
|
| 219 |
+
3 API çağrısı, 6 chunk:
|
| 220 |
+
1) heading1_part1_api_call => chunk #1, chunk #2
|
| 221 |
+
2) heading2_3_api_call => chunk #3, chunk #4
|
| 222 |
+
3) heading4_expansion_api_call => chunk #5, chunk #6
|
|
|
|
| 223 |
"""
|
| 224 |
+
# API Call #1 => chunk#1, chunk#2
|
| 225 |
+
heading1_text = heading1_part1_api_call(input_text)
|
| 226 |
+
|
| 227 |
+
# API Call #2 => chunk#3, chunk#4
|
| 228 |
+
heading2_text, heading3_text = heading2_3_api_call(heading1_text)
|
| 229 |
+
|
| 230 |
+
# API Call #3 => chunk#5, chunk#6
|
| 231 |
+
final_text = heading4_expansion_api_call(
|
| 232 |
+
heading1_text=heading1_text,
|
| 233 |
+
heading2_text=heading2_text,
|
| 234 |
+
heading3_text=heading3_text,
|
| 235 |
+
input_text=input_text
|
| 236 |
+
)
|
| 237 |
|
| 238 |
+
# HTML
|
| 239 |
+
final_html = final_text.replace("\n","<br>")
|
| 240 |
+
# Word count
|
| 241 |
+
plain = re.sub(r"<.*?>","", final_text)
|
| 242 |
+
wcount = len(plain.split())
|
| 243 |
|
| 244 |
+
info = f"✅ Done. Final text has ~{wcount} words."
|
| 245 |
+
return final_html, info
|
| 246 |
|
| 247 |
+
def run_gradio_app(user_text, file):
|
| 248 |
+
content = process_input_text(file, user_text)
|
| 249 |
+
if not content:
|
| 250 |
+
return ("⚠️ Please provide text or file input!", "")
|
|
|
|
| 251 |
|
| 252 |
+
final_html, info = main_pipeline(content)
|
| 253 |
+
return (final_html, info)
|
|
|
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
+
# ============== Gradio Arayüz ================
|
| 257 |
+
def build_app():
|
|
|
|
| 258 |
text_input = gr.Textbox(
|
| 259 |
lines=5,
|
| 260 |
label="Text Input (Optional)",
|
| 261 |
+
placeholder="Write or paste text here..."
|
| 262 |
)
|
| 263 |
file_input = gr.File(
|
| 264 |
+
label="Upload PDF/DOCX/TXT",
|
| 265 |
+
file_types=[".pdf",".docx",".txt"]
|
| 266 |
)
|
| 267 |
+
output_html = gr.HTML(label="Final Output (4 headings, 4000-10000 words)")
|
| 268 |
+
info_label = gr.Label(label="Information")
|
|
|
|
| 269 |
|
|
|
|
| 270 |
demo = gr.Interface(
|
| 271 |
+
fn=run_gradio_app,
|
| 272 |
inputs=[text_input, file_input],
|
| 273 |
outputs=[output_html, info_label],
|
| 274 |
+
title="6-Chunks in 3 API Calls (gpt-4o-mini)",
|
| 275 |
description=(
|
| 276 |
+
"Human-coded example with 3 separate API calls to produce 6 chunks:\n"
|
| 277 |
+
"API #1 => chunk#1-2 (Heading1 in 2 parts)\n"
|
| 278 |
+
"API #2 => chunk#3-4 (Heading2 & Heading3)\n"
|
| 279 |
+
"API #3 => chunk#5-6 (Heading4 & expansions for 4000-10000 words range)\n"
|
| 280 |
+
"(No AI used in writing this code. It's purely developer-coded!)"
|
| 281 |
)
|
| 282 |
)
|
| 283 |
return demo
|
| 284 |
|
|
|
|
| 285 |
if __name__ == "__main__":
|
| 286 |
+
app = build_app()
|
| 287 |
+
app.launch()
|
|
|
|
|
|