Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Set up Groq API key
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Use environment variable for security
|
| 7 |
+
client = openai.OpenAI(api_key=GROQ_API_KEY, base_url="https://api.groq.com/openai/v1")
|
| 8 |
+
|
| 9 |
+
# Initialize conversation history
|
| 10 |
+
conversation_history = []
|
| 11 |
+
|
| 12 |
+
def chat_with_groq(user_input):
|
| 13 |
+
global conversation_history
|
| 14 |
+
|
| 15 |
+
if user_input.lower() == 'exit':
|
| 16 |
+
return "Chatbot: Goodbye!"
|
| 17 |
+
|
| 18 |
+
conversation_history.append({"role": "user", "content": user_input})
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
response = client.chat.completions.create(
|
| 22 |
+
model="llama3-8b-8192", # Replace with your desired model
|
| 23 |
+
messages=conversation_history
|
| 24 |
+
)
|
| 25 |
+
bot_reply = response.choices[0].message.content
|
| 26 |
+
conversation_history.append({"role": "assistant", "content": bot_reply})
|
| 27 |
+
return bot_reply
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Error: {e}"
|
| 30 |
+
|
| 31 |
+
# Create Gradio interface
|
| 32 |
+
chatbot_ui = gr.Interface(
|
| 33 |
+
fn=chat_with_groq,
|
| 34 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your message..."),
|
| 35 |
+
outputs=gr.Textbox(),
|
| 36 |
+
title="Groq Chatbot",
|
| 37 |
+
description="Chat with an AI powered by Groq API!"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
chatbot_ui.launch(share=True)
|