|
|
|
|
|
|
|
|
|
|
|
|
|
|
| import os
|
| import requests
|
| import gradio as gr
|
| from dotenv import load_dotenv
|
|
|
|
|
| load_dotenv()
|
|
|
|
|
| groq_api_key = os.environ.get("GROQ_API_KEY")
|
|
|
|
|
| if groq_api_key is None:
|
| raise ValueError("Groq API key is not set in environment variables.")
|
|
|
|
|
| url = "https://api.groq.com/openai/v1/chat/completions"
|
|
|
|
|
| def groq_chat(prompt):
|
| headers = {
|
| "Authorization": f"Bearer {groq_api_key}"
|
| }
|
| body = {
|
| "model": "llama-3.1-8b-instant",
|
| "messages": [
|
| {
|
| "role": "user",
|
| "content": prompt
|
| }
|
| ]
|
| }
|
|
|
|
|
| response = requests.post(url, headers=headers, json=body)
|
|
|
| if response.status_code == 200:
|
|
|
| return response.json().get('choices', [{}])[0].get('message', {}).get('content', "No response found.")
|
| else:
|
|
|
| return f"Error {response.status_code}: {response.text}"
|
|
|
|
|
| with gr.Blocks() as interface:
|
| gr.Markdown("# DDS 1st Chatbot")
|
| with gr.Row():
|
| user_input = gr.Textbox(label="Enter your prompt", placeholder="Type something funny or interesting...")
|
| with gr.Row():
|
| output = gr.Textbox(label="Response from Groq API")
|
| with gr.Row():
|
| submit_button = gr.Button("Get Response")
|
|
|
| submit_button.click(fn=groq_chat, inputs=user_input, outputs=output)
|
|
|
|
|
| if __name__ == "__main__":
|
| interface.launch(share=True) |