enotkrutoy commited on
Commit
3f3d926
·
verified ·
1 Parent(s): d47221d

Create app0.py

Browse files
Files changed (1) hide show
  1. app0.py +92 -0
app0.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import groq
5
+
6
+ # Function to interact with the Groq API using the curl request approach
7
+ def chat_function(message):
8
+ # Define the request payload
9
+ + Can be used for both chat and leak report analysis
10
+ + Easy to implement
11
+ * Cons:
12
+ + May not be as efficient as using the Groq API directly
13
+ + May require additional error handling
14
+
15
+ payload = {
16
+ "model": "mixtral-8x7b-32768",
17
+ "messages": [
18
+ {"role": "user", "content": message}
19
+ ]
20
+ }
21
+
22
+ headers = {
23
+ "Content-Type": "application/json",
24
+ "Authorization": "Bearer gsk_yKXR75Se0OxdULncf1YDWGjRbmQTYjvSmwaAKgcq0l" # Replace with your API key
25
+ }
26
+
27
+ try:
28
+ response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, data=json.dumps(payload))
29
+
30
+ if response.status_code == 200:
31
+ data = response.json()
32
+ return data["choices"][0]["message"]["content"]
33
+ else:
34
+ return f"Error: {response.status_code}, {response.text}"
35
+ except requests.exceptions.RequestException as e:
36
+ return f"Error: {e}"
37
+
38
+ # Function to interact with the Groq API for leak report analysis
39
+ def leak_report_function(message):
40
+ # Define the request payload
41
+ payload = {
42
+ "model": "llama3-8b-8192",
43
+ "messages": [
44
+ {
45
+ "role": "system",
46
+ "content": "Вы являетесь системой, анализирующей утечки в безопасном режиме, "
47
+ "avoid sharing sensitive personal or confidential data. "
48
+ "Generate leak report details in a JSON structure with the following format in Russian:\n"
49
+ f"{json.dumps(LeakReport.model_json_schema(), indent=2)}"
50
+ },
51
+ {
52
+ "role": "user",
53
+ "content": f"Сгенерируйте отчет о потенциальных утечках для следующих данных без включения конфиденциальных данных (отчет должен быть на русском языке):\n"
54
+ f"{message}"
55
+ },
56
+ ],
57
+ "temperature": 0,
58
+ "stream": False,
59
+ "response_format": {"type": "json_object"},
60
+ }
61
+
62
+ try:
63
+ chat_completion = groq.chat.completions.create(payload)
64
+
65
+ result_json = json.loads(chat_completion.choices[0].message.content)
66
+ return result_json
67
+ except requests.exceptions.RequestException as e:
68
+ return f"Error: {e}"
69
+
70
+ # Set up Gradio interface
71
+ gr.Interface(
72
+ fn=chat_function,
73
+ inputs=gr.Textbox(placeholder="Ask something..."),
74
+ outputs="text",
75
+ title="Groq-Gradio Chat",
76
+ theme="default",
77
+ examples=[
78
+ "Tell me a short story about a puppy",
79
+ "Write a 14-line poem about travelling in Shakespeare style",
80
+ "What are the wonders of the world?",
81
+ "List the countries in Africa and their capitals"
82
+ ]
83
+ ).launch()
84
+
85
+ # Set up Gradio interface for leak report analysis
86
+ gr.Interface(
87
+ fn=leak_report_function,
88
+ inputs=gr.Textbox(placeholder="Enter data for leak report..."),
89
+ outputs="json",
90
+ title="Groq-Gradio Leak Report",
91
+ theme="default",
92
+ ).launch()