File size: 4,456 Bytes
f4a091c
 
 
 
 
 
 
 
 
df0f591
 
 
 
 
f4a091c
 
 
 
 
 
 
 
 
 
 
2826193
f4a091c
fc0cdf2
f4a091c
fc0cdf2
340532d
f4a091c
fc0cdf2
f4a091c
fc0cdf2
f4a091c
fc0cdf2
f4a091c
fc0cdf2
f4a091c
 
fc0cdf2
f4a091c
 
fc0cdf2
f4a091c
fc0cdf2
f4a091c
 
 
 
 
 
21a3057
f4a091c
fc0cdf2
 
 
 
21a3057
5500e55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ededac
5500e55
 
 
 
 
 
 
 
2ededac
5500e55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e76a6f
f4a091c
21a3057
 
 
 
 
 
2d06113
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
"""Copy of Copy of Chatbot with custom knowledge base

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1VSXUmag_76fzebs16YhW_as4mdhHNdkx
"""

#pip install llama-index
#pip install langchain
#pip install gradio
#pip install pandas 
#pip install openpyxl

import pandas as pd
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import sys
import os
from IPython.display import Markdown, display
import pandas as pd
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
from IPython.display import Markdown, display
#import streamlit as st
import gradio as gr
#import gradio
df = pd.read_excel('Shegardi_dataset.xlsx',sheet_name = 'dataset')
#os.environ['OPENAI_API_KEY'] = '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'
os.environ['OPENAI_API_KEY'] = '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'
def construct_index(directory_path):
    # set maximum input size
    max_input_size = 4096
    # set number of output tokens
    num_outputs = 2000
    # set maximum chunk overlap
    max_chunk_overlap = 20
    # set chunk size limit
    chunk_size_limit = 600 

    # define LLM
    llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="text-davinci-003", max_tokens=num_outputs))
    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
 
    documents = SimpleDirectoryReader(directory_path).load_data()
    
    index = GPTSimpleVectorIndex(
        documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper
    )

    index.save_to_disk('index.json')

    return index

#construct_index("context_data/data")

#import streamlit as st
# Include other necessary imports here

# Add the cashback calculator
def cashback_calculator(segment, total_spent, international_transactions, local_transactions):
    cashback = 0

    if find_similar(segment, ['almassy', 'alsafwa_plus', 'safwa', 'w', 'W']):
        if total_spent < 3000:
            cashback = total_spent * 0.03
        else:
            cashback = international_transactions * 0.06 + local_transactions * 0.03
            
        if find_similar(segment, ['almassy', 'alsafwa_plus', 'safwa']):
            max_cashback = 500
        else:  # segment == 'w'
            max_cashback = 300
            
        cashback = min(cashback, max_cashback)
        
    elif similar(segment, 'normal'):
        cashback = total_spent * 0.01
        cashback = min(cashback, 150)
    
    return cashback

# Helper functions
def similar(a, b, threshold=0.5):
    return SequenceMatcher(None, a, b).ratio() > threshold

def find_similar(segment, segments):
    return any(similar(segment, s) for s in segments)

def is_query_about_cashback(query):
    cashback_keywords = ["cashback", "calculate", "calculation", "reward", "points"]
    return any(word.lower() in query.lower() for word in cashback_keywords)

# Main language model function
def ask_ai(query):
    if is_query_about_cashback(query):
        # Extract the required information from the query or ask the user for more information if needed
        segment = input("Enter your card segment: ")
        total_spent = float(input("Enter your total spent amount: "))
        international_transactions = float(input("Enter your international transactions amount: "))
        local_transactions = float(input("Enter your local transactions amount: "))

        cashback = cashback_calculator(segment, total_spent, international_transactions, local_transactions)
        return f"The cashback amount for your card is: {cashback:.2f}"
    else:
        index = GPTSimpleVectorIndex.load_from_disk('index.json')
        response = index.query(query, response_mode="compact")
        return response.response   


# Gradio interface
import gradio as gr
iface = gr.Interface(fn=ask_ai, inputs="text", outputs="text", title="The following is a conversation with a human called Shegardi. Shegardi is helpful, precise, truthful, and very friendly. Also, Shegardi is an employee of Warba Bank, located in Kuwait. Shegardi will only use the information provided to him.",
                     description="Enter a question and get an answer from Shegardi.")
iface.launch()