|
|
import gradio as gr |
|
|
import os |
|
|
import requests |
|
|
|
|
|
|
|
|
GROQ_API_KEY = os.getenv("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") |
|
|
MODEL_NAME = "llama3-70b-8192" |
|
|
|
|
|
|
|
|
TASK_TEMPLATES = { |
|
|
"Generate Content": "Write a detailed and engaging piece of content about the following topic:\n\n{input}", |
|
|
"Paraphrase": "Paraphrase the following text in a clear and concise way:\n\n{input}", |
|
|
"Summarize": "Summarize the following content in a few key points:\n\n{input}", |
|
|
"Correct Grammar": "Fix grammatical mistakes and rewrite this text properly:\n\n{input}", |
|
|
"Change Tone": "Rewrite the following text in a {tone} tone:\n\n{input}" |
|
|
} |
|
|
|
|
|
def query_groq_model(prompt, temperature=0.7): |
|
|
"""Send request to Groq API.""" |
|
|
url = "https://api.groq.com/openai/v1/chat/completions" |
|
|
headers = { |
|
|
"Authorization": "Bearer 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", |
|
|
"Content-Type": "application/json" |
|
|
} |
|
|
payload = { |
|
|
"model": MODEL_NAME, |
|
|
"messages": [{"role": "user", "content": prompt}], |
|
|
"temperature": temperature, |
|
|
} |
|
|
|
|
|
response = requests.post(url, headers=headers, json=payload) |
|
|
result = response.json() |
|
|
return result['choices'][0]['message']['content'] |
|
|
|
|
|
def process_text(task, user_input, tone): |
|
|
if not user_input.strip(): |
|
|
return "❗ Please enter some text." |
|
|
|
|
|
if task == "Change Tone": |
|
|
prompt = TASK_TEMPLATES[task].format(input=user_input, tone=tone) |
|
|
else: |
|
|
prompt = TASK_TEMPLATES[task].format(input=user_input) |
|
|
|
|
|
try: |
|
|
output = query_groq_model(prompt) |
|
|
except Exception as e: |
|
|
output = f"⚠️ Error: {str(e)}" |
|
|
return output |
|
|
|
|
|
|
|
|
with gr.Blocks(title="Content Creation & Paraphrasing Tool") as demo: |
|
|
gr.Markdown("## ✍️ Content Creation & Paraphrasing Tool (Open Source + Groq API)") |
|
|
|
|
|
with gr.Row(): |
|
|
task = gr.Dropdown( |
|
|
label="Select Task", |
|
|
choices=list(TASK_TEMPLATES.keys()), |
|
|
value="Generate Content" |
|
|
) |
|
|
tone = gr.Dropdown( |
|
|
label="Tone (if applicable)", |
|
|
choices=["formal", "casual", "professional", "friendly"], |
|
|
visible=False |
|
|
) |
|
|
|
|
|
user_input = gr.Textbox(label="Input Text or Topic", lines=6, placeholder="Enter your text here...") |
|
|
output = gr.Textbox(label="Output", lines=8) |
|
|
|
|
|
task.change(lambda t: gr.update(visible=(t == "Change Tone")), inputs=task, outputs=tone) |
|
|
|
|
|
run_btn = gr.Button("Submit") |
|
|
run_btn.click(fn=process_text, inputs=[task, user_input, tone], outputs=output) |
|
|
|
|
|
gr.Markdown("Made with ❤️ using Open Source Models + Groq + Gradio") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|