keremberke commited on
Commit
1b46501
1 Parent(s): 2aa0e82

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +29 -9
  2. db_utils.py +64 -0
  3. gpt_utils.py +21 -0
  4. requirements.txt +4 -1
app.py CHANGED
@@ -1,18 +1,38 @@
1
  import gradio as gr
2
  import dotenv
3
  import os
 
 
4
 
5
  dotenv.load_dotenv()
6
  PW=os.getenv("PW")
7
 
8
- def greet(name, request: gr.Request):
9
- print(request.username)
10
- return "Hello " + name + "!"
11
 
12
- with gr.Blocks() as demo:
13
- name = gr.Textbox(label="Name")
14
- output = gr.Textbox(label="Output Box")
15
- greet_btn = gr.Button("Greet")
16
- greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet")
17
 
18
- demo.launch(auth=[("kerem", PW), ("kerem2", PW)],favicon_path="./tugra_logo.png")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import dotenv
3
  import os
4
+ from gpt_utils import update_text_with_ai
5
+ from db_utils import supabase, get_next_row_id, get_text_by_row_id
6
 
7
  dotenv.load_dotenv()
8
  PW=os.getenv("PW")
9
 
10
+ def apply_ai_func(text, request: gr.Request):
11
+ updated_text2 = update_text_with_ai(text)
12
+ return updated_text2
13
 
14
+ def add_to_db_func(text, request: gr.Request):
15
+ #data, count = supabase.table('eso-tr').update({'updated_text': text}).eq('id', 1).execute()
 
 
 
16
 
17
+
18
+ pass
19
+
20
+ def get_next_func(request: gr.Request):
21
+ next_id = get_next_row_id(user_name=request.username)
22
+ text=get_text_by_row_id(next_id)
23
+ return next_id, text
24
+
25
+ with gr.Blocks(analytics_enabled=False) as demo:
26
+ get_from_db = gr.Button("🗂️Sıradaki Veriyi Çek🗂️")
27
+ with gr.Row():
28
+ text_id = gr.Number(label="Satır NO:", interactive=False,scale= 1)
29
+ original_text = gr.Textbox(label="Orjinial Yazı:", interactive=False,scale= 5)
30
+ apply_ai = gr.Button("✨BÜYÜ YAP✨")
31
+ updated_text = gr.Textbox(label="Düzeltilmiş Yazı:")
32
+ add_to_db = gr.Button("📥Veri Tabanına Yükle📥")
33
+
34
+ get_from_db.click(fn=get_next_func, outputs=[text_id, original_text])
35
+ apply_ai.click(fn=apply_ai_func, inputs=original_text, outputs=updated_text)
36
+ add_to_db.click(fn=add_to_db_func, inputs=updated_text)
37
+
38
+ demo.launch(auth=[("kerem", PW), ("mehmet", PW)],favicon_path="./tugra_logo.png")
db_utils.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import random
4
+ from supabase.client import ClientOptions
5
+ from supabase import create_client, Client
6
+ from dotenv import load_dotenv
7
+
8
+ load_dotenv()
9
+
10
+ user_name_to_range = {'kerem': (1, 1000), 'mehmet': (1001, 2001), 'ege': (2001, 1013817)}
11
+
12
+ url: str = os.environ.get("SUPABASE_URL")
13
+ key: str = os.environ.get("SUPABASE_KEY")
14
+ supabase: Client = create_client(url, key, options=ClientOptions(postgrest_client_timeout=300))
15
+
16
+
17
+ def eso_replace_string(from_: str, to_: str, text: str):
18
+ # Regex pattern:
19
+ # \w*1\w* - En az bir '1' içeren kelimeleri bulur.
20
+ # Ancak bu, sadece "1" içeren tek karakterleri de içerebilir ve bu yüzden toplam uzunluğu kontrol ediyoruz.
21
+ pattern = fr'\b\w*{from_}\w*\b'
22
+
23
+ # Metindeki tüm kelimeler üzerinde dön
24
+ for word in re.findall(pattern, text):
25
+ # Eğer kelime en az 2 karakter içeriyorsa ve en az bir harf içeriyorsa
26
+ if len(word) > 1 and re.search("[a-zA-Z]", word):
27
+ modified_word = word.replace(from_, to_)
28
+ text = text.replace(word, modified_word)
29
+ return text
30
+
31
+
32
+ # Dosyayı açıp içeriğini okuyan ve içeriği bir string olarak döndüren fonksiyon
33
+ def dosyayi_oku(dosya_yolu):
34
+ try:
35
+ with open(dosya_yolu, 'r', encoding='iso-8859-9') as dosya:
36
+ icerik = dosya.read()
37
+ return icerik
38
+ except FileNotFoundError:
39
+ print("Dosya bulunamadı.")
40
+ except Exception as e:
41
+ print(f"Bir hata oluştu: {e}")
42
+
43
+
44
+ def get_next_row_id(user_name: str):
45
+ row_range = user_name_to_range[user_name]
46
+ data, count = supabase.table('eso-tr').select('id').gte('id', row_range[0]).lt('id', row_range[1]).eq('done', False).limit(1).execute()
47
+ text_id = data[1][0]['id']
48
+ return text_id
49
+
50
+
51
+ def get_text_by_row_id(id: int):
52
+ data, count = supabase.table('eso-tr').select('text').eq('id', id).limit(1).execute()
53
+ text = data[1][0]['text']
54
+ return text
55
+
56
+
57
+
58
+
59
+ def add_updated_text_to_db(text: str):
60
+ pass
61
+
62
+
63
+
64
+
gpt_utils.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from litellm import completion
2
+ import os
3
+ from dotenv import load_dotenv
4
+ import litellm
5
+
6
+ load_dotenv()
7
+
8
+ system_prompt = """
9
+ You are ChatGPT, a large language model trained by OpenAI.
10
+ Carefully heed the user's instructions.
11
+ Respond using Markdown.you know turkish
12
+ sana verilen cumledeki hatalari gidericeksin
13
+ 1 yerine ı, 0 yerine İ, _ yerine ş,  yerine ğ, ^ yerine Ş yerleştirilecek
14
+ & ve \n işaretlerini değiştirme
15
+ """
16
+
17
+
18
+ def update_text_with_ai(text: str):
19
+ response = completion("azure/gpt-4-0125-preview", messages = [{ "content": system_prompt,"role": "system"},{ "content": text,"role": "user"}])
20
+ updated_text = response.choices[0].text['content']
21
+ return updated_text
requirements.txt CHANGED
@@ -1,2 +1,5 @@
1
  gradio==4.18.0
2
- python-dotenv
 
 
 
 
1
  gradio==4.18.0
2
+ python-dotenv
3
+ supabase
4
+ tqdm
5
+ litelmm=1.24.3