File size: 970 Bytes
48f2381
 
8a025d6
48f2381
8a025d6
 
6c844a8
6a93b02
6c844a8
8a025d6
5d4b003
8a025d6
5d4b003
 
48f2381
8a025d6
 
 
 
 
6c844a8
 
8a025d6
48f2381
8a025d6
 
 
 
 
b6cfcdc
8a025d6
 
 
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
import os
import requests
import gradio as gr

# Get API key from environment variable
API_KEY = os.environ.get("GROQ_API_KEY")
API_URL = "https://api.groq.com/v1/chat/completions"  # <-- FIXED

# Function to call GROQ API
def query_groq(prompt):
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    data = {
        "model": "llama3-8b-8192",
        "messages": [
            {"role": "system", "content": "You are an expert Programming Tutor."},
            {"role": "user", "content": prompt}
        ],
        "temperature": 0.7
    }

    response = requests.post(API_URL, headers=headers, json=data)
    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"]
    else:
        return f"Error: {response.status_code} - {response.text}"

# Gradio interface
iface = gr.Interface(fn=query_groq, inputs="text", outputs="text", title="Groq Chat")
iface.launch()