Spaces:
Sleeping
Sleeping
import os | |
from openai import OpenAI | |
# Ensure the OPENROUTER_API_KEY environment variable is set | |
api_key = "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" | |
if not api_key: | |
raise ValueError("OPENROUTER_API_KEY environment variable not set.") | |
# Point the OpenAI client to the OpenRouter API | |
client = OpenAI( | |
base_url="https://openrouter.ai/api/v1", | |
api_key=api_key, | |
) | |
def generate_synthetic_text(prompt: str, model: str = "deepseek/deepseek-chat-v3-0324:free") -> str: | |
""" | |
Generates synthetic text using an OpenRouter model. | |
Args: | |
prompt: The input prompt to guide the text generation. | |
model: The model to use on OpenRouter (default: gpt-3.5-turbo). | |
You can find model names on the OpenRouter website. | |
Returns: | |
The generated text string. | |
""" | |
try: | |
response = client.chat.completions.create( | |
extra_headers={ | |
# "HTTP-Referer": "https://www.google.com", # Optional. Site URL for rankings on openrouter.ai. | |
"X-Title": "SynthGen", # Optional. Site title for rankings on openrouter.ai. | |
}, | |
model=model, | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant generating synthetic data."}, | |
{"role": "user", "content": prompt}, | |
], | |
) | |
if response.choices and response.choices[0].message.content: | |
return response.choices[0].message.content.strip() | |
else: | |
return "Error: No content generated." | |
except Exception as e: | |
return f"Error during API call: {e}" | |
# --- Main Execution --- | |
if __name__ == "__main__": | |
# TODO: Define the kind of text and number of samples needed | |
num_samples = 5 # Example: generate 5 samples | |
prompt_template = "Generate a short, positive product review for a fictional gadget." # Example prompt | |
print(f"Generating {num_samples} synthetic text samples...") | |
for i in range(num_samples): | |
# You might want to vary the prompt slightly for each sample | |
# For now, we use the same template | |
generated_text = generate_synthetic_text(prompt_template) | |
print(f"\n--- Sample {i+1} ---") | |
print(generated_text) | |
print("\nGeneration complete.") | |