File size: 2,830 Bytes
c488daa
 
 
 
 
a2f2677
c488daa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d40d285
c488daa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import gradio as gr
import os
import requests

# Set your Groq API Key
GROQ_API_KEY = os.getenv("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")  # or hardcode temporarily
MODEL_NAME = "llama3-70b-8192"  # You can also use "mixtral-8x7b-32768" or "gemma-7b-it"

# Define task prompts
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

# Gradio UI
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()