Spaces:
Sleeping
Sleeping
File size: 6,805 Bytes
33f677a cc0c2a6 33f677a cc0c2a6 33f677a 23e3a6c 33f677a 9e17941 cc0c2a6 33f677a 23e3a6c cc0c2a6 aa5b105 23e3a6c 2e6c205 33f677a 23e3a6c dbd9712 23e3a6c 33f677a cc0c2a6 33f677a dbd9712 23e3a6c dbd9712 23e3a6c dbd9712 2e6c205 dbd9712 cc0c2a6 33f677a dbd9712 2e6c205 33f677a dbd9712 33f677a dbd9712 2e6c205 dbd9712 33f677a dbd9712 33f677a dbd9712 33f677a 2e6c205 33f677a dbd9712 33f677a cc0c2a6 2e6c205 33f677a dbd9712 33f677a dbd9712 cc0c2a6 2e6c205 dbd9712 33f677a dbd9712 cc0c2a6 33f677a dbd9712 cc0c2a6 dbd9712 cc0c2a6 2e6c205 dbd9712 cc0c2a6 dbd9712 cc0c2a6 dbd9712 cc0c2a6 33f677a 2e6c205 33f677a dbd9712 33f677a dbd9712 cc0c2a6 dbd9712 cc0c2a6 dbd9712 cc0c2a6 dbd9712 cc0c2a6 dbd9712 2e6c205 dbd9712 cc0c2a6 2e6c205 23e3a6c cc0c2a6 2e6c205 33f677a cc0c2a6 2e6c205 cc0c2a6 33f677a dbd9712 cc0c2a6 2e6c205 dbd9712 2e6c205 33f677a dbd9712 33f677a dbd9712 cc0c2a6 33f677a dbd9712 23e3a6c dbd9712 23e3a6c dbd9712 23e3a6c dbd9712 2e6c205 33f677a dbd9712 2e6c205 dbd9712 2e6c205 dbd9712 2e6c205 dbd9712 33f677a 2e6c205 dbd9712 cc0c2a6 33f677a dbd9712 33f677a dbd9712 33f677a dbd9712 2855466 dbd9712 33f677a 23e3a6c dbd9712 2e6c205 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# app.py
# -------------------------------------------------------------------------
# NOT: Bu kod tamamen insan (developer) tarafından yazılmıştır, GPT veya
# başka bir yapay zeka tarafından üretilmemiştir. Eğitim amaçlı paylaşılmaktadır.
#
# Model: gpt-4o-mini
# Min. 4000 kelime, Max. 10000 kelime
# 3 ayrı API çağrısı, her çağrıda 2 chunk -> 6 chunk toplam
# -------------------------------------------------------------------------
import os
import re
import gradio as gr
# Gerekli kütüphaneler
try:
from openai import OpenAI
import tiktoken
from PyPDF2 import PdfReader
from docx import Document
except ImportError:
raise ImportError("Lütfen 'openai', 'tiktoken', 'gradio', 'PyPDF2', 'python-docx' paketlerini kurun.")
# ============== 1) OPENAI API İstemcisi ================
client = OpenAI(api_key="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")
def call_openai_chat(messages, max_tokens=10000, temperature=0.8):
"""
gpt-4o-mini modeline istek atar.
- max_tokens=10000 => uzun metin
- temperature=0.8 => daha yaratıcı
"""
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
stop=None
)
return response.choices[0].message.content
# ============== 2) Chunk Fonksiyonları ===============
def heading1_part1_and_part2(input_text):
"""
API Çağrısı #1 => 2 chunk (Heading 1 Part1 + Part2)
Part1 ~1000 kelime, Part2 => final ~2000 kelime
"""
# chunk #1 => part1
prompt1 = f"""
We want Heading 1 (introductory overview) in two parts.
PART 1 => around 1000+ words. NOT final.
Input:
{input_text}
"""
msgs1 = [
{"role": "system", "content": "You are an AI assistant creating heading1 part1."},
{"role": "user", "content": prompt1}
]
h1_part1 = call_openai_chat(msgs1)
# chunk #2 => part2 => finalize
prompt2 = f"""
Partial heading1:
{h1_part1}
Now finalize heading1 with part2.
Ensure total ~2000+ words. Return final heading1 only.
"""
msgs2 = [
{"role": "system", "content": "You are finalizing heading #1."},
{"role": "user", "content": prompt2}
]
heading1_final = call_openai_chat(msgs2)
return heading1_final
def heading2_and_3_api(heading1_text):
"""
API Çağrısı #2 => 2 chunk (Heading2, Heading3)
chunk #3 => heading2
chunk #4 => heading3
"""
# heading2
prompt_h2 = f"""
We have heading1 for context.
Produce 'Heading 2: Detailed explanation of common risks.' ~1000+ words.
Return heading2 text only.
Context sample:
{heading1_text[:1500]}
"""
msgs_h2 = [
{"role": "system", "content": "You are creating heading2."},
{"role": "user", "content": prompt_h2}
]
heading2_text = call_openai_chat(msgs_h2)
# heading3
prompt_h3 = f"""
We have heading1 for context.
Produce 'Heading 3: Practical examples and solutions.' ~1000+ words.
Return heading3 text only.
Context sample:
{heading1_text[:1500]}
"""
msgs_h3 = [
{"role": "system", "content": "You are creating heading3."},
{"role": "user", "content": prompt_h3}
]
heading3_text = call_openai_chat(msgs_h3)
return heading2_text, heading3_text
def heading4_and_expansion_api(h1_text, h2_text, h3_text, original_input):
"""
API Çağrısı #3 => 2 chunk (Heading4, expansions/shorten)
chunk #5 => heading4
chunk #6 => expansions if <4000 words, or shorten if >10000
"""
# chunk #5 => heading4
prompt_h4 = f"""
We have heading1,2,3.
Produce 'Heading 4: Summary and next steps for students.' ~1000 words at least.
Return heading4 only.
Context sample:
{h1_text[:1200]}
"""
msgs_h4 = [
{"role": "system", "content": "You are creating heading4."},
{"role": "user", "content": prompt_h4}
]
heading4_text = call_openai_chat(msgs_h4)
# chunk #6 => expansions or shorten
prompt_expand = f"""
We have 4 headings now:
[Heading1]
{h1_text}
[Heading2]
{h2_text}
[Heading3]
{h3_text}
[Heading4]
{heading4_text}
Combine them into one final text.
If total < 4000 words => expand.
If > 10000 => shorten.
Return final text only, merged.
Original input:
{original_input}
"""
msgs_expand = [
{"role": "system", "content": "You ensure final word count 4000-10000."},
{"role": "user", "content": prompt_expand}
]
final_text = call_openai_chat(msgs_expand)
return final_text
# ============== 3) Pipeline (6 chunk, 3 API çağrısı) ==============
def main_pipeline(input_txt):
"""
3 API Çağrısı:
1) heading1_part1_and_part2 => chunk #1 + #2
2) heading2_and_3_api => chunk #3 + #4
3) heading4_and_expansion_api => chunk #5 + #6
"""
# API #1 => Heading1
heading1_text = heading1_part1_and_part2(input_txt)
# API #2 => Heading2, Heading3
heading2_text, heading3_text = heading2_and_3_api(heading1_text)
# API #3 => Heading4 + expansions
final_text = heading4_and_expansion_api(
h1_text=heading1_text,
h2_text=heading2_text,
h3_text=heading3_text,
original_input=input_txt
)
return final_text
# ============== 4) Gradio Arayüz Fonksiyonları ==============
def run_pipeline(user_input_text):
"""
Tek girdi: user_input_text (string).
Dönüş: final_html, info_label
"""
if not user_input_text.strip():
return ("⚠️ Please provide some text!", "")
# pipeline
final_text = main_pipeline(user_input_text)
# HTML
final_html = final_text.replace("\n","<br>")
# Word count
plain_text = re.sub(r"<.*?>","", final_text)
wcount = len(plain_text.split())
info = f"✅ Done. Final text ~{wcount} words (target 4000-10000)."
return (final_html, info)
def build_app():
text_input = gr.Textbox(
lines=5,
label="Input Text (Minimum 4000 words, maximum 10000 words in final result)",
placeholder="Paste or type your input text here..."
)
output_html = gr.HTML(label="Final Output")
output_info = gr.Label(label="Information (Word Count)")
demo = gr.Interface(
fn=run_pipeline,
inputs=text_input,
outputs=[output_html, output_info],
title="6 Chunks with 3 API Calls (gpt-4o-mini)",
description=(
"3 API calls, each producing 2 chunks => 6 total.\n"
"Heading1 in 2 parts, then heading2+3, then heading4+expansions.\n"
"Ensures at least 4000 words, max 10000 words.\n"
)
)
return demo
if __name__ == "__main__":
app = build_app()
app.launch() |