|
import os |
|
import requests |
|
import gradio as gr |
|
|
|
|
|
groq_api_key = os.getenv("GROQ_API_KEY") |
|
|
|
if not groq_api_key: |
|
raise ValueError("GROQ_API_KEY is missing! Set it in the Hugging Face Spaces 'Secrets'.") |
|
|
|
|
|
url = "https://api.groq.com/openai/v1/chat/completions" |
|
headers = {"Authorization": f"Bearer {groq_api_key}"} |
|
|
|
|
|
def chat_with_groq(user_input): |
|
body = { |
|
"model": "deepseek-r1-distill-qwen-32b", |
|
"messages": [{"role": "user", "content": user_input}] |
|
} |
|
|
|
response = requests.post(url, headers=headers, json=body) |
|
|
|
if response.status_code == 200: |
|
return response.json()['choices'][0]['message']['content'] |
|
else: |
|
return f"Error: {response.json()}" |
|
|
|
|
|
interface = gr.Interface( |
|
fn=chat_with_groq, |
|
inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."), |
|
outputs=gr.Textbox(), |
|
title="Chat with Groq AI (deepseek-r1-distill-qwen-32b)", |
|
description="Type your question below and get a response powered by Groq's deepseek-r1-distill-qwen-32b model." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |