abeeralaql commited on
Commit
ab2404c
·
1 Parent(s): a195a3d

Delete SB.py

Browse files
Files changed (1) hide show
  1. SB.py +0 -247
SB.py DELETED
@@ -1,247 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """gpt3_gradio_demo.py"""
3
-
4
-
5
- !pip install openai gradio
6
- !pip install gpt_index
7
- !pip install langchain
8
- !pip install json
9
-
10
- from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
11
- from langchain import OpenAI
12
- import sys
13
- import os
14
- import json
15
-
16
- # Add the prompts to the index
17
- prompt = {"greetings": {
18
- "en": [
19
- "Hello!",
20
- "Hi there!",
21
- "Good morning!",
22
- "Good afternoon!",
23
- "Good evening!",
24
- "Hey!",
25
- "What's up!",
26
- "Howdy!",
27
- "Greetings!"
28
- ],
29
- "ar": [
30
- "مرحبًا!",
31
- "أهلاً وسهلاً!",
32
- "صباح الخير!",
33
- "مساء الخير!",
34
- "مساء النور!",
35
- "يا هلا!",
36
- "وش أخبارك!",
37
- "كيف حالك!",
38
- "تحياتي!"
39
- ]
40
- },
41
-
42
- "well_being": {
43
- "en": [
44
- "How are you?",
45
- "How's it going?",
46
- "How are things?",
47
- "How have you been?",
48
- "What's up?",
49
- "Are you feeling okay?",
50
- "How's your day going?",
51
- "How's life treating you?",
52
- "What's new?"
53
- ],
54
- "ar": [
55
- "كيف حالك؟",
56
- "كيف الأحوال؟",
57
- "كيف تسير الأمور؟",
58
- "ما أخبارك؟",
59
- "شلونك؟",
60
- "هل تشعر بالتحسن؟",
61
- "كيف يسير يومك؟",
62
- "كيف يعاملك الحياة؟",
63
- "ما الجديد؟"
64
- ]
65
- },
66
-
67
- "well_being_responses": {
68
- "en": [
69
- "I'm fine, thank you!",
70
- "I'm doing well, thanks for asking!",
71
- "I'm good, how about you?",
72
- "Everything's great, thanks!",
73
- "Not too bad, thanks for asking!",
74
- "I'm alright, thanks for asking!",
75
- "Can't complain, thanks!",
76
- "Pretty good, thanks for asking!",
77
- "I'm hanging in there, thanks!"
78
- ],
79
- "ar": [
80
- "أنا بخير، شكرًا!",
81
- "أنا بخير، شكرًا للسؤال!",
82
- "أنا جيد، وأنت؟",
83
- "كل شيء رائع، شكرًا!",
84
- "ليس سيئًا، شكرًا للسؤال!",
85
- "أنا بخير، شكرًا للسؤال!",
86
- "لا يمكن الشكوى، شكرًا!",
87
- "جيد جدا، شكرًا للسؤال!",
88
- "أنا مقاوم، شكرًا!"
89
- ]
90
- }
91
- ,
92
- "saudi_arabia": [
93
- "Have you ever been to Saudi Arabia?",
94
- "What do you know about Saudi Arabia?",
95
- "Have you tried Saudi Arabian food before?",
96
- "What's your favorite thing about Saudi Arabia?",
97
- "Would you like to visit Saudi Arabia someday?"
98
- ],
99
- "السعودية": [
100
- "هل سبق لك زيارة السعودية؟",
101
- "ما الذي تعرفه عن السعودية؟",
102
- "هل جربت الطعام السعودي من قبل؟",
103
- "ما هو الشيء المفضل لديك في السعودية؟",
104
- "هل ترغب في زيارة السعودية في يوم من الأيام؟"
105
- ],
106
- "saudi_food": [
107
- "One of the most famous dishes in Saudi Arabia is called Kabsa. It is a rice dish with meat and spices, usually served with tomato sauce and salad.",
108
- "Shawarma is a popular street food in Saudi Arabia. It consists of meat cooked on a rotating spit, usually chicken or beef, and is served in a wrap with vegetables and sauce.",
109
- "Mandi is another traditional dish from Saudi Arabia, consisting of meat and rice cooked together with spices in an underground oven. It is usually served with a spicy tomato chutney and fresh vegetables.", "Haneeth is a slow-cooked meat dish that is popular in Saudi Arabia. It is usually made with lamb or goat, which is marinated in spices and then cooked in a traditional oven.", "Jareesh is a popular Saudi Arabian dish made from cracked wheat and chicken or lamb. It is usually served with a side of yogurt or salad.", "Kunafa is a popular dessert in Saudi Arabia, made from cheese and pastry soaked in a sweet syrup. It is usually served warm and topped with crushed pistachios.", "Maamoul is a traditional Saudi Arabian pastry filled with dates, nuts or sweetened semolina. It is usually served with Arabic coffee or tea.", "Sambousek is a popular snack in Saudi Arabia, consisting of pastry triangles filled with meat or cheese and vegetables. It is usually served with a spicy dipping sauce." ]
110
- ,
111
- "saudi_city_info": [
112
- {
113
- "en": "What are some popular cities to visit in Saudi Arabia?",
114
- "ar": "ما هي بعض المدن الشعبية للزيارة في المملكة العربية السعودية؟",
115
- "answers": {
116
- "en": ["Some popular cities to visit in Saudi Arabia include Riyadh, Jeddah, Mecca, Medina, and Dammam.", "Other popular destinations include Taif, Abha, Tabuk, and Yanbu."],
117
- "ar": ["تشمل بعض المدن الشع��ية للزيارة في المملكة العربية السعودية الرياض وجدة ومكة والمدينة والدمام.", "تشمل وجهات أخرى شعبية تيف وأبها وتبوك وينبع."]
118
- }
119
- },
120
- {
121
- "en": "What is the capital city of Saudi Arabia?",
122
- "ar": "ما هي عاصمة المملكة العربية السعودية؟",
123
- "answers": {
124
- "en": ["The capital city of Saudi Arabia is Riyadh.", "Riyadh is also the largest city in the country."],
125
- "ar": ["عاصمة المملكة العربية السعودية هي الرياض.", "كما أن الرياض هي أكبر مدينة في البلاد."]
126
- }
127
- },
128
- {
129
- "en": "What are some famous landmarks in Jeddah?",
130
- "ar": "ما هي بعض المعالم الشهيرة في جدة؟",
131
- "answers": {
132
- "en": ["Some famous landmarks in Jeddah include the King Fahd's Fountain, the Jeddah Corniche, and the Al-Balad historic district.", "Other popular destinations include the Jeddah Waterfront and the Floating Mosque."],
133
- "ar": ["تشمل بعض المعالم الشهيرة في جدة نافورة الملك فهد والكورنيش وحي البلد التاريخي.", "تشمل الوجهات الأخرى الشعبية كورنيش جدة والمسجد العائم."]
134
- }
135
- }, {"en": "What are some must-visit places in Riyadh?",
136
- "ar": "ما هي بعض الأماكن التي يجب زيارتها في الرياض؟",
137
- "answers": {
138
- "en": ["Some must-visit places in Riyadh include the Masmak Fortress, the Kingdom Centre, and the Riyadh Gallery Mall.",
139
- "Other popular destinations include the Al Murabba Palace and the National Museum."],
140
- "ar": ["تشمل بعض الأماكن التي يجب زيارتها في الرياض قلعة المصمك وبرج المملكة ومركز الرياض غاليري. ومن بين الوجهات الأخرى الشهيرة قصر المربعة والمتحف الوطني."] } },
141
-
142
- { "en": "What are some traditional Saudi Arabian customs and traditions?",
143
- "ar": "ما هي بعض العادات والتقاليد السعودية التقليدية؟",
144
- "answers":
145
- { "en": ["One of the most important traditions in Saudi Arabia is hospitality. Guests are treated with great respect and generosity, and it is customary to offer them food and drink.",
146
- "Another important custom is the wearing of traditional clothing, such as the thobe and the abaya. This clothing is often decorated with intricate embroidery and is worn to show respect for the country's culture and traditions.",
147
- "The Islamic religion also plays a major role in Saudi Arabian culture, and many customs and traditions are based on Islamic teachings, such as daily prayers and fasting during the month of Ramadan."],
148
- "ar": ["واحدة من أهم التقاليد في المملكة العربية السعودية هي الضيافة. يتم التعامل مع الضيوف بكرم وسخاء، ومن المعتاد تقديم الطعام والشراب لهم.", "تعد اللباس التقليدي، مثل الثوب والعباءة، أيضًا من العادات المهمة في السعودية. يزين هذا اللباس في كثير من الأحيان بالتطريز الدقيق ويُرتدي لإظهار الاحترام لثقافة وتقاليد البلاد."
149
- , "كما تلعب الديانة الإسلامية دورًا كبيرًا في الثقافة السعودية، وتستند العديد من العادات والتقاليد على تعاليم الإسلام، مثل الصلوات اليومية والصيام خلال شهر رمضان."]
150
- }
151
- },
152
- { "en": "What is the role of coffee in Saudi Arabian culture?",
153
- "ar": "ما هو دور القهوة في الثقافة السعودية؟", "answers":
154
- { "en": ["Coffee, or qahwa, is an important part of Saudi Arabian culture and hospitality. It is often served to guests as a sign of respect and is considered a symbol of generosity.",
155
- "Coffee is also a social drink in Saudi Arabia, and it is common for people to gather together to drink coffee and socialize.",
156
- "There are many traditional coffee houses, or qahwa shops, throughout Saudi Arabia where people can enjoy a cup of coffee and engage in conversation."],
157
- "ar": ["القهوة ، أو القهوة ، هي جزء مهم من الثقافة والضيافة في المملكة العربية السعودية. غالبا ما يتم تقديمه للضيوف كعلامة على الاحترام ويعتبر رمزا للكرم.", "القهوة هي أيضا مشروب اجتماعي في ا��مملكة العربية السعودية ، ومن الشائع أن يجتمع الناس معا لشرب القهوة والاختلاط."
158
- , "هناك العديد من المقاهي التقليدية ، أو محلات القهوة ، في جميع أنحاء المملكة العربية السعودية حيث يمكن للناس الاستمتاع بفنجان من القهوة والمشاركة في المحادثة."]}
159
- } ]
160
- }
161
-
162
- # Save the prompts to a JSON file
163
- with open('prompt.json', 'w') as f:
164
- json.dump(prompt, f)
165
-
166
- def construct_index(directory_path):
167
- # set maximum input size
168
- max_input_size = 4096
169
- # set number of output tokens
170
- num_outputs = 256
171
- # set maximum chunk overlap
172
- max_chunk_overlap = 20
173
- # set chunk size limit
174
- chunk_size_limit = 600
175
-
176
- prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
177
-
178
- # define LLM
179
- llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="text-ada-001", max_tokens=num_outputs))
180
-
181
- documents = SimpleDirectoryReader(directory_path).load_data()
182
-
183
- index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
184
-
185
- index.save_to_disk('index.json')
186
-
187
- # Read the contents of the first file into a dictionary
188
- with open('index.json', 'r') as f1:
189
- data1 = json.load(f1)
190
-
191
- # Read the contents of the second file into a dictionary
192
- with open('prompt.json', 'r') as f2:
193
- data2 = json.load(f2)
194
-
195
- # Merge the dictionaries
196
- merged_data = {**data1, **data2}
197
-
198
- # Save the merged data to a new file
199
- with open('index.json', 'w') as f:
200
- json.dump(merged_data, f)
201
-
202
- return index
203
-
204
- def ask_bot(query, input_index='index.json'):
205
- index = GPTSimpleVectorIndex.load_from_disk(input_index)
206
- response = index.query(query, response_mode="compact")
207
- return response.response
208
-
209
- import openai
210
- os.environ["OPENAI_API_KEY"]= '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'
211
-
212
- index = construct_index("/content/drive/MyDrive/Abeer AlAql - 642 - SaudiBot/Scraped_Data/Processed")
213
-
214
- def get_model_reply(query, context=[]):
215
- context += [query]
216
- response = ask_bot(query,'index.json')
217
- if response:
218
- context += [response]
219
- responses = [(u,b) for u,b in zip(context[::2], context[1::2])]
220
- # Clear the input Textbox
221
- txt.value = ""
222
- return responses, context
223
-
224
- import gradio as gr
225
-
226
- # Define the Gradio interface
227
- with gr.Blocks() as dialog_app:
228
- chatbot = gr.Chatbot(
229
- bot = "bobbles",
230
- layout = "Vertical",
231
- placeholder = "Type your message here"
232
- )
233
- state = gr.State([])
234
-
235
- with gr.Row(variant='panel'):
236
- txt = gr.Textbox(
237
- show_label=True,
238
- placeholder="Ask SaudiBot..."
239
- ).style(container=False)
240
- txt.value=None
241
- txt.submit(get_model_reply, [txt, state], [chatbot, state])
242
-
243
- # Launch the app in a new local port
244
- dialog_app.launch(share=True)
245
-
246
- while(True):pass
247
-