File size: 2,118 Bytes
d21d216
 
 
 
 
 
b4c95f4
d21d216
 
 
 
b4c95f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d21d216
 
b4c95f4
d21d216
b4c95f4
d21d216
 
b4c95f4
 
d21d216
 
 
 
b4c95f4
d21d216
 
b4c95f4
d21d216
 
 
 
 
 
 
b4c95f4
d21d216
 
 
 
b4c95f4
d21d216
 
 
 
 
 
 
 
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
79
80
import os
import gradio as gr
from groq import Groq

api_key = os.getenv("GROQ_API_KEY")
if not api_key:
    raise ValueError("GROQ_API_KEY not found in environment variables.")

client = Groq(api_key=api_key)
MODEL_NAME = "llama-3.1-8b-instant"

def normalize_history(history):
    """
    Convert Gradio history to (user, assistant) pairs.
    Handles both:
    - [["hi", "hello"], ["ok", "sure"]]
    - [{"role":"user","content":"hi"}, {"role":"assistant","content":"hello"}, ...]
    """
    normalized = []

    if not history:
        return normalized

    if isinstance(history[0], list):
        # Format: [["hi", "hello"], ["ok", "sure"]]
        return history

    # Format: [{"role": "...", "content": "..."}]
    temp_pair = []
    for msg in history:
        if msg["role"] == "user":
            temp_pair = [msg["content"], None]
        elif msg["role"] == "assistant":
            if temp_pair:
                temp_pair[1] = msg["content"]
                normalized.append(temp_pair)
                temp_pair = []

    return normalized


def chat_groq(message, history):
    try:
        history_pairs = normalize_history(history)

        system_prompt = "You are a helpful AI assistant."
        messages = [{"role": "system", "content": system_prompt}]

        # Add normalized history
        for user_msg, bot_msg in history_pairs:
            messages.append({"role": "user", "content": user_msg})
            if bot_msg:
                messages.append({"role": "assistant", "content": bot_msg})

        # Add latest message
        messages.append({"role": "user", "content": message})

        # Groq API
        completion = client.chat.completions.create(
            model=MODEL_NAME,
            messages=messages,
            max_tokens=400,
            temperature=0.7,
        )

        return completion.choices[0].message.content

    except Exception as e:
        return f"Error: {e}"


ui = gr.ChatInterface(
    fn=chat_groq,
    title="Sajid's GenAI App (Groq)",
    description="Simple Llama-3 chat app running on Groq.",
)

if __name__ == "__main__":
    ui.launch()