ramalMr commited on
Commit
d53066f
·
verified ·
1 Parent(s): 34421df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -35
app.py CHANGED
@@ -29,44 +29,33 @@ def generate(file, temperature, max_new_tokens, top_p, repetition_penalty):
29
  sentences = text.split('.')
30
  random.shuffle(sentences) # Shuffle sentences
31
 
32
- # CSV dosyası için başlık
33
- if not os.path.exists("synthetic_data.csv"):
34
- with open("synthetic_data.csv", mode='w', newline='', encoding='utf-8') as file:
35
- writer = csv.writer(file)
36
- writer.writerow(["Original Sentence", "Synthetic Data"])
 
 
 
37
 
38
- for sentence in sentences:
39
- sentence = sentence.strip()
40
- if not sentence:
41
- continue
 
 
 
 
42
 
43
- generate_kwargs = {
44
- "temperature": temperature,
45
- "max_new_tokens": max_new_tokens,
46
- "top_p": top_p,
47
- "repetition_penalty": repetition_penalty,
48
- "do_sample": True,
49
- "seed": 42,
50
- }
51
 
52
- try:
53
- stream = client.text_generation(sentence, **generate_kwargs, stream=True, details=True, return_full_text=False)
54
- output = ""
55
- for response in stream:
56
- output += response.token.text
57
- save_to_csv(sentence, output)
58
- except Exception as e:
59
- print(f"Error generating data for sentence '{sentence}': {e}")
60
- save_to_csv(sentence, f"Error: {e}")
61
-
62
- # CSV dosyasını okuyup byte olarak döndür
63
- with open("synthetic_data.csv", "r", encoding="utf-8") as file:
64
- csv_content = file.read()
65
- csv_bytes = csv_content.encode()
66
-
67
- # Geçici dosya oluştur ve içeriği yaz
68
- with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
69
- tmp.write(csv_bytes)
70
  tmp_path = tmp.name
71
 
72
  return tmp_path
 
29
  sentences = text.split('.')
30
  random.shuffle(sentences) # Shuffle sentences
31
 
32
+ # Geçici dosya oluştur ve CSV yazıcısını başlat
33
+ with tempfile.NamedTemporaryFile(mode='w', newline='', delete=False, suffix='.csv') as tmp:
34
+ writer = csv.writer(tmp)
35
+
36
+ for sentence in sentences:
37
+ sentence = sentence.strip()
38
+ if not sentence:
39
+ continue
40
 
41
+ generate_kwargs = {
42
+ "temperature": temperature,
43
+ "max_new_tokens": max_new_tokens,
44
+ "top_p": top_p,
45
+ "repetition_penalty": repetition_penalty,
46
+ "do_sample": True,
47
+ "seed": 42,
48
+ }
49
 
50
+ try:
51
+ stream = client.text_generation(sentence, **generate_kwargs, stream=True, details=True, return_full_text=False)
52
+ output = ""
53
+ for response in stream:
54
+ output += response.token.text
55
+ writer.writerow([sentence, output]) # Orijinal cümle ve yanıt CSV'ye yazılır
56
+ except Exception as e:
57
+ print(f"Error generating data for sentence '{sentence}': {e}")
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  tmp_path = tmp.name
60
 
61
  return tmp_path