Spaces:
Running
Running
Upload synthgen.py
Browse files- synthgen.py +61 -0
synthgen.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from openai import OpenAI
|
3 |
+
|
4 |
+
# Ensure the OPENROUTER_API_KEY environment variable is set
|
5 |
+
api_key = "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"
|
6 |
+
if not api_key:
|
7 |
+
raise ValueError("OPENROUTER_API_KEY environment variable not set.")
|
8 |
+
|
9 |
+
# Point the OpenAI client to the OpenRouter API
|
10 |
+
client = OpenAI(
|
11 |
+
base_url="https://openrouter.ai/api/v1",
|
12 |
+
api_key=api_key,
|
13 |
+
)
|
14 |
+
|
15 |
+
def generate_synthetic_text(prompt: str, model: str = "deepseek/deepseek-chat-v3-0324:free") -> str:
|
16 |
+
"""
|
17 |
+
Generates synthetic text using an OpenRouter model.
|
18 |
+
|
19 |
+
Args:
|
20 |
+
prompt: The input prompt to guide the text generation.
|
21 |
+
model: The model to use on OpenRouter (default: gpt-3.5-turbo).
|
22 |
+
You can find model names on the OpenRouter website.
|
23 |
+
|
24 |
+
Returns:
|
25 |
+
The generated text string.
|
26 |
+
"""
|
27 |
+
try:
|
28 |
+
response = client.chat.completions.create(
|
29 |
+
extra_headers={
|
30 |
+
# "HTTP-Referer": "https://www.google.com", # Optional. Site URL for rankings on openrouter.ai.
|
31 |
+
"X-Title": "SynthGen", # Optional. Site title for rankings on openrouter.ai.
|
32 |
+
},
|
33 |
+
model=model,
|
34 |
+
messages=[
|
35 |
+
{"role": "system", "content": "You are a helpful assistant generating synthetic data."},
|
36 |
+
{"role": "user", "content": prompt},
|
37 |
+
],
|
38 |
+
)
|
39 |
+
if response.choices and response.choices[0].message.content:
|
40 |
+
return response.choices[0].message.content.strip()
|
41 |
+
else:
|
42 |
+
return "Error: No content generated."
|
43 |
+
except Exception as e:
|
44 |
+
return f"Error during API call: {e}"
|
45 |
+
|
46 |
+
# --- Main Execution ---
|
47 |
+
if __name__ == "__main__":
|
48 |
+
# TODO: Define the kind of text and number of samples needed
|
49 |
+
num_samples = 5 # Example: generate 5 samples
|
50 |
+
prompt_template = "Generate a short, positive product review for a fictional gadget." # Example prompt
|
51 |
+
|
52 |
+
print(f"Generating {num_samples} synthetic text samples...")
|
53 |
+
|
54 |
+
for i in range(num_samples):
|
55 |
+
# You might want to vary the prompt slightly for each sample
|
56 |
+
# For now, we use the same template
|
57 |
+
generated_text = generate_synthetic_text(prompt_template)
|
58 |
+
print(f"\n--- Sample {i+1} ---")
|
59 |
+
print(generated_text)
|
60 |
+
|
61 |
+
print("\nGeneration complete.")
|