Spaces:
Sleeping
Sleeping
Commit ·
a4b071c
1
Parent(s): dfcc918
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
"""
|
| 4 |
+
Created on Tue Apr 25 04:44:31 2023
|
| 5 |
+
|
| 6 |
+
@author: mai2125
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from gpt_index import (
|
| 10 |
+
SimpleWebPageReader,
|
| 11 |
+
WikipediaReader,
|
| 12 |
+
GPTListIndex,
|
| 13 |
+
GPTSimpleVectorIndex,
|
| 14 |
+
LLMPredictor,
|
| 15 |
+
QuestionAnswerPrompt,
|
| 16 |
+
PromptHelper
|
| 17 |
+
)
|
| 18 |
+
from langchain.chat_models import ChatOpenAI
|
| 19 |
+
from langchain.prompts.chat import ChatPromptTemplate, SystemMessagePromptTemplate
|
| 20 |
+
import gradio as gr
|
| 21 |
+
import docx2txt
|
| 22 |
+
import sys
|
| 23 |
+
import os
|
| 24 |
+
# openai api key
|
| 25 |
+
os.environ["OPENAI_API_KEY"] = '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'
|
| 26 |
+
|
| 27 |
+
def chatbot(input_text):
|
| 28 |
+
# preset prompt
|
| 29 |
+
|
| 30 |
+
query_str = "What state is Atlanta in?"
|
| 31 |
+
QA_PROMPT_TMPL = (
|
| 32 |
+
"You are an AI specialized in Atlanta.\n"
|
| 33 |
+
"We have provided context information below. \n"
|
| 34 |
+
"---------------------\n"
|
| 35 |
+
"{context_str}"
|
| 36 |
+
"\n---------------------\n"
|
| 37 |
+
"Given this information, please answer the question: {query_str}\n"
|
| 38 |
+
"If this question definitely does not involve Atlanta, say I am a chatbot designed to answer queries about specifically Atlanta.\n"
|
| 39 |
+
)
|
| 40 |
+
QA_PROMPT = QuestionAnswerPrompt(QA_PROMPT_TMPL)
|
| 41 |
+
# Takes in the input from the user to deliver responses
|
| 42 |
+
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
| 43 |
+
response = index.query(input_text, text_qa_template = QA_PROMPT)
|
| 44 |
+
return response.response
|
| 45 |
+
|
| 46 |
+
# creates the interface for the user to interact with the chatbot
|
| 47 |
+
iface = gr.Interface(fn=chatbot,
|
| 48 |
+
inputs=gr.components.Textbox(lines=7, label="Enter your text"),
|
| 49 |
+
outputs="text",
|
| 50 |
+
title="PD4 Chatbot based on Atlanta Wiki")
|
| 51 |
+
|
| 52 |
+
iface.launch()
|