akhaliq HF staff commited on
Commit
c80c142
1 Parent(s): 88d1ec7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+ import os
4
+
5
+ def chatbot(message, history, api_key):
6
+ # Initialize Groq client with the provided API key
7
+ client = Groq(api_key=api_key)
8
+
9
+ # Prepare the messages including the conversation history
10
+ messages = [
11
+ {"role": "system", "content": "You are a helpful assistant."}
12
+ ]
13
+ for human, assistant in history:
14
+ messages.append({"role": "user", "content": human})
15
+ messages.append({"role": "assistant", "content": assistant})
16
+ messages.append({"role": "user", "content": message})
17
+
18
+ try:
19
+ # Create the chat completion
20
+ completion = client.chat.completions.create(
21
+ model="llama-3.2-90b-text-preview",
22
+ messages=messages,
23
+ temperature=1,
24
+ max_tokens=1024,
25
+ top_p=1,
26
+ stream=True,
27
+ stop=None,
28
+ )
29
+
30
+ # Stream the response
31
+ partial_message = ""
32
+ for chunk in completion:
33
+ if chunk.choices[0].delta.content is not None:
34
+ partial_message += chunk.choices[0].delta.content
35
+ yield partial_message
36
+ except Exception as e:
37
+ yield f"Error: {str(e)}"
38
+
39
+ # Create the Gradio interface
40
+ with gr.Blocks(theme="soft") as iface:
41
+ gr.Markdown("# Groq LLaMA 3.2 90B Chatbot")
42
+ gr.Markdown("Chat with the LLaMA 3.2 90B model using Groq API")
43
+
44
+ with gr.Row():
45
+ api_key_input = gr.Textbox(
46
+ label="Enter your Groq API Key",
47
+ placeholder="sk-...",
48
+ type="password"
49
+ )
50
+
51
+ chatbot = gr.ChatInterface(
52
+ chatbot,
53
+ additional_inputs=[api_key_input],
54
+ examples=[
55
+ "Tell me a short story about a robot learning to paint.",
56
+ "Explain quantum computing in simple terms.",
57
+ "What are some creative ways to reduce plastic waste?",
58
+ ],
59
+ retry_btn=None,
60
+ undo_btn="Delete Last",
61
+ clear_btn="Clear",
62
+ )
63
+
64
+ # Launch the interface
65
+ iface.launch()