Spaces:
Sleeping
Sleeping
harishreddy311 commited on
Commit ·
94900aa
0
Parent(s):
Intial Commit
Browse files
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
GROQ_API_KEY=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
|
README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Groq Chat Assistant
|
| 2 |
+
|
| 3 |
+
A chat assistant powered by Groq's Llama 3.3 70B model, built with Gradio.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- Real-time chat interface
|
| 8 |
+
- Context-aware conversations
|
| 9 |
+
- Powered by Groq's high-performance LLM
|
| 10 |
+
- Beautiful and responsive UI
|
| 11 |
+
|
| 12 |
+
## Local Development
|
| 13 |
+
|
| 14 |
+
1. Clone this repository
|
| 15 |
+
2. Install dependencies:
|
| 16 |
+
```bash
|
| 17 |
+
pip install -r requirements.txt
|
| 18 |
+
```
|
| 19 |
+
3. Create a `.env` file with your Groq API key:
|
| 20 |
+
```
|
| 21 |
+
GROQ_API_KEY=your_api_key_here
|
| 22 |
+
```
|
| 23 |
+
4. Run the application:
|
| 24 |
+
```bash
|
| 25 |
+
python app.py
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
## Deployment to Hugging Face Spaces
|
| 29 |
+
|
| 30 |
+
1. Create a new Space on Hugging Face
|
| 31 |
+
2. Set up your Space with the following settings:
|
| 32 |
+
- SDK: Gradio
|
| 33 |
+
- Space hardware: CPU Basic
|
| 34 |
+
3. Add your Groq API key as a Space secret:
|
| 35 |
+
- Go to Settings → Secrets
|
| 36 |
+
- Add a new secret with key `GROQ_API_KEY` and your API key as the value
|
| 37 |
+
4. Push your code to the Space repository
|
| 38 |
+
|
| 39 |
+
## Environment Variables
|
| 40 |
+
|
| 41 |
+
- `GROQ_API_KEY`: Your Groq API key (required)
|
| 42 |
+
|
| 43 |
+
## License
|
| 44 |
+
|
| 45 |
+
MIT
|
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import gradio as gr # type: ignore
|
| 3 |
+
import os
|
| 4 |
+
from groq import Groq # type: ignore
|
| 5 |
+
import logging
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Set up logging
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
def get_groq_client():
|
| 16 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
| 17 |
+
if not api_key:
|
| 18 |
+
logger.error("GROQ_API_KEY environment variable is not set")
|
| 19 |
+
raise ValueError("GROQ_API_KEY environment variable is not set. Please set it in the Hugging Face Space secrets.")
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
client = Groq(api_key=api_key)
|
| 23 |
+
logger.info("Successfully initialized Groq client")
|
| 24 |
+
return client
|
| 25 |
+
except Exception as e:
|
| 26 |
+
logger.error(f"Failed to initialize Groq client: {str(e)}")
|
| 27 |
+
raise
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
client = get_groq_client()
|
| 31 |
+
except Exception as e:
|
| 32 |
+
logger.error(f"Application failed to start: {str(e)}")
|
| 33 |
+
raise
|
| 34 |
+
|
| 35 |
+
def random_response(message, history):
|
| 36 |
+
try:
|
| 37 |
+
# Convert history to the format expected by Groq API
|
| 38 |
+
messages = []
|
| 39 |
+
|
| 40 |
+
# Process history from Gradio format
|
| 41 |
+
for msg in history:
|
| 42 |
+
if isinstance(msg, dict) and 'role' in msg and 'content' in msg:
|
| 43 |
+
role = 'assistant' if msg['role'] == 'assistant' else 'user'
|
| 44 |
+
messages.append({"role": role, "content": msg['content']})
|
| 45 |
+
else:
|
| 46 |
+
logger.warning(f"Skipping invalid message format: {msg}")
|
| 47 |
+
|
| 48 |
+
# Add the current message
|
| 49 |
+
messages.append({"role": "user", "content": message})
|
| 50 |
+
|
| 51 |
+
logger.info(f"Sending request to Groq API with {len(messages)} messages")
|
| 52 |
+
|
| 53 |
+
chat_completion = client.chat.completions.create(
|
| 54 |
+
messages=messages,
|
| 55 |
+
model="llama-3.3-70b-versatile",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
response = chat_completion.choices[0].message.content
|
| 59 |
+
logger.info("Successfully received response from Groq API")
|
| 60 |
+
return response
|
| 61 |
+
except Exception as e:
|
| 62 |
+
logger.error(f"Error in random_response: {str(e)}")
|
| 63 |
+
return f"Error: {str(e)}"
|
| 64 |
+
|
| 65 |
+
# Create the Gradio interface
|
| 66 |
+
demo = gr.ChatInterface(
|
| 67 |
+
random_response,
|
| 68 |
+
title="Groq Chat Assistant",
|
| 69 |
+
description="A chat assistant powered by Groq's Llama 3.3 70B model",
|
| 70 |
+
examples=["What is the capital of France?", "Tell me a joke", "Explain quantum computing"],
|
| 71 |
+
theme=gr.themes.Soft(),
|
| 72 |
+
retry_btn=None,
|
| 73 |
+
undo_btn=None,
|
| 74 |
+
clear_btn="Clear Chat",
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
logger.info("Starting Gradio application")
|
| 79 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
groq>=0.4.0
|
| 3 |
+
python-dotenv>=1.0.0
|