zabi8 commited on
Commit
f8bc09f
·
verified ·
1 Parent(s): 8e34bcb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+ GROQ_API_KEY = "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"
6
+ HEADERS = {"Authorization": f"Bearer {GROQ_API_KEY}"}
7
+
8
+
9
+ suggested_questions = [
10
+ "How is my TFSA performing?",
11
+ "What are some good stock picks?",
12
+ "Compare investment strategies.",
13
+ "Analyze stock volatility."
14
+ ]
15
+
16
+ def ask_groq(query, language="English"):
17
+ api_url = "https://api.groq.com/v1/chat/completions"
18
+ payload = {
19
+ "model": "groq-llm", # Replace with actual Groq model name
20
+ "messages": [{"role": "user", "content": query}],
21
+ "max_tokens": 200,
22
+ "temperature": 0.7
23
+ }
24
+
25
+ response = requests.post(api_url, headers=HEADERS, json=payload)
26
+ if response.status_code == 200:
27
+ reply = response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response.")
28
+ return f"{reply}\n\n(Language: {language})"
29
+ else:
30
+ return "Error: Could not retrieve response."
31
+
32
+ def chatbot(user_input, language="English"):
33
+ if user_input in suggested_questions:
34
+ return ask_groq(user_input, language)
35
+ return ask_groq(user_input, language)
36
+
37
+ with gr.Blocks() as demo:
38
+ gr.Markdown("# TFSA Investment Chatbot 💰")
39
+
40
+ language = gr.Radio(["English", "Français"], label="Select Language", value="English")
41
+
42
+ with gr.Row():
43
+ input_text = gr.Textbox(label="Ask a question", placeholder="Type your question here...")
44
+ submit_btn = gr.Button("Ask")
45
+
46
+ response_output = gr.Textbox(label="Response", interactive=False)
47
+
48
+ gr.Markdown("### Suggested Questions:")
49
+ btns = [gr.Button(q) for q in suggested_questions]
50
+
51
+ # Event Listeners
52
+ submit_btn.click(fn=chatbot, inputs=[input_text, language], outputs=response_output)
53
+
54
+ for btn in btns:
55
+ btn.click(fn=chatbot, inputs=[btn, language], outputs=response_output)
56
+
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()