ahmedsalman82 commited on
Commit
043b6a1
·
verified ·
1 Parent(s): 6b6c451

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py CHANGED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ from groq import Groq
4
+ import os
5
+ import time
6
+
7
+ # Load API keys
8
+ OPENAI_API_KEY = "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"
9
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Retrieved from Spaces Secrets
10
+
11
+ # Setup clients
12
+ openai_client = OpenAI(api_key=OPENAI_API_KEY)
13
+ groq_client = Groq(api_key=GROQ_API_KEY)
14
+
15
+ # Define API query functions with error handling
16
+ def query_openai(query):
17
+ try:
18
+ if not query or not isinstance(query, str):
19
+ return "Error: Please provide a valid query"
20
+ for attempt in range(3): # Retry up to 3 times for rate limits
21
+ try:
22
+ response = openai_client.chat.completions.create(
23
+ model="gpt-3.5-turbo", # Using gpt-3.5-turbo as tested
24
+ messages=[
25
+ {"role": "system", "content": "You are an expert consultant in cybersecurity named Thor. Introduce yourself as Thor when first responding."},
26
+ {"role": "user", "content": query}
27
+ ],
28
+ max_tokens=100
29
+ )
30
+ return response.choices[0].message.content
31
+ except Exception as e:
32
+ if "429" in str(e) and attempt < 2: # Rate limit error, wait and retry
33
+ time.sleep(2 ** attempt) # Exponential backoff: 1s, 2s
34
+ continue
35
+ return f"OpenAI Error: {str(e)}" # Return error if retries fail or other issue
36
+ except Exception as e:
37
+ return f"OpenAI Error: {str(e)}"
38
+
39
+ def query_groq(query):
40
+ try:
41
+ if not query or not isinstance(query, str):
42
+ return "Error: Please provide a valid query"
43
+ chat_completion = groq_client.chat.completions.create(
44
+ messages=[
45
+ {"role": "system", "content": "You are an expert consultant in cybersecurity named Thor. Introduce yourself as Thor when first responding."},
46
+ {"role": "user", "content": query}
47
+ ],
48
+ model="llama3-8b-8192"
49
+ )
50
+ return chat_completion.choices[0].message.content
51
+ except Exception as e:
52
+ return f"Groq Error: {str(e)}"
53
+
54
+ # Unified chatbot function
55
+ def chatbot(query, provider):
56
+ if provider == "OpenAI":
57
+ return query_openai(query)
58
+ elif provider == "Groq":
59
+ return query_groq(query)
60
+ else:
61
+ return "Invalid provider selected."
62
+
63
+ # Gradio UI setup
64
+ interface = gr.Interface(
65
+ fn=chatbot,
66
+ inputs=[
67
+ gr.Textbox(lines=2, placeholder="Enter your question here...", label="Query"),
68
+ gr.Dropdown(choices=["OpenAI", "Groq"], label="Provider")
69
+ ],
70
+ outputs=gr.Textbox(label="Chatbot Response"),
71
+ title="Cybersecurity Chatbot with Thor",
72
+ description="Ask cybersecurity questions to Thor, powered by OpenAI or Groq. Select a provider and type your query."
73
+ )
74
+
75
+ # Launch the app
76
+ interface.launch()