sksin commited on
Commit
815fc0b
1 Parent(s): 28b6142

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -43
app.py CHANGED
@@ -1,44 +1,41 @@
 
 
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import torch
4
-
5
- model = AutoModelForCausalLM.from_pretrained(
6
- "tiiuae/falcon-7b-instruct",
7
- torch_dtype=torch.bfloat16,
8
- trust_remote_code=True,
9
- device_map="auto",
10
- low_cpu_mem_usage=True,
11
- )
12
- tokenizer = AutoTokenizer.from_pretrained("tiiuae/falcon-7b-instruct")
13
-
14
-
15
- def generate_text(input_text):
16
- input_ids = tokenizer.encode(input_text, return_tensors="pt")
17
- attention_mask = torch.ones(input_ids.shape)
18
-
19
- output = model.generate(
20
- input_ids,
21
- attention_mask=attention_mask,
22
- max_length=200,
23
- do_sample=True,
24
- top_k=10,
25
- num_return_sequences=1,
26
- eos_token_id=tokenizer.eos_token_id,
27
- )
28
-
29
- output_text = tokenizer.decode(output[0], skip_special_tokens=True)
30
- print(output_text)
31
-
32
- # Remove Prompt Echo from Generated Text
33
- cleaned_output_text = output_text.replace(input_text, "")
34
- return cleaned_output_text
35
-
36
-
37
- text_generation_interface = gr.Interface(
38
- fn=generate_text,
39
- inputs=[
40
- gr.inputs.Textbox(label="Input Text"),
41
- ],
42
- outputs=gr.inputs.Textbox(label="Generated Text"),
43
- title="Falcon-7B Instruct",
44
- ).launch()
 
1
+ from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
2
+ from langchain.chat_models import ChatOpenAI
3
  import gradio as gr
4
+ import sys
5
+ import os
6
+ import openai
7
+
8
+ os.environ["OPENAI_API_KEY"] = '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'
9
+ openai.api_key = "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"
10
+
11
+ def construct_index(directory_path):
12
+ max_input_size = 4096
13
+ num_outputs = 512
14
+ max_chunk_overlap = 20
15
+ chunk_size_limit = 600
16
+
17
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
18
+
19
+ llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
20
+
21
+ documents = SimpleDirectoryReader(directory_path).load_data()
22
+
23
+ index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
24
+
25
+ index.save_to_disk('index.json')
26
+
27
+ return index
28
+
29
+
30
+ def chatbot(input_text):
31
+ index = GPTSimpleVectorIndex.load_from_disk('index.json')
32
+ response = index.query(input_text, response_mode="compact")
33
+ return response.response
34
+
35
+ iface = gr.Interface(fn=chatbot,
36
+ inputs=gr.components.Textbox(lines=7, label="Enter your text"),
37
+ outputs="text",
38
+ title="Custom-trained AI Chatbot")
39
+
40
+ index = construct_index("docs")
41
+ iface.launch(share=True)