Delete app2.py
Browse files
app2.py
DELETED
|
@@ -1,63 +0,0 @@
|
|
| 1 |
-
import openai
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import docx # For reading Word documents
|
| 4 |
-
|
| 5 |
-
openai.api_key = "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"
|
| 6 |
-
|
| 7 |
-
messages = [
|
| 8 |
-
{"role": "system", "content": "You are a chatbot that is very happy and kind. You will search the website https://www.patch-and-sparks.com/en/"},
|
| 9 |
-
]
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def read_word_document(file_path):
|
| 13 |
-
#Read and return all paragraphs from the Word document.
|
| 14 |
-
doc = docx.Document(file_path)
|
| 15 |
-
paragraphs = []
|
| 16 |
-
for paragraph in doc.paragraphs:
|
| 17 |
-
paragraphs.append(paragraph.text)
|
| 18 |
-
return paragraphs
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def search_in_doc(paragraphs, search_term):
|
| 22 |
-
|
| 23 |
-
#Search for the term in each paragraph of the document and return the matching paragraph.
|
| 24 |
-
for paragraph in paragraphs:
|
| 25 |
-
if search_term.lower() in paragraph.lower():
|
| 26 |
-
return f"I found this information in the document: '{paragraph}'"
|
| 27 |
-
return "Sorry, I couldn't find that information in the document."
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def chatbot(input):
|
| 32 |
-
if input:
|
| 33 |
-
# Add user input to the chat
|
| 34 |
-
messages.append({"role": "user", "content": input})
|
| 35 |
-
|
| 36 |
-
# Search for the information in the Word document
|
| 37 |
-
doc_content = read_word_document("./Info_1.docx") # Specify the path to your Word document
|
| 38 |
-
doc_reply = search_in_doc(doc_content, input)
|
| 39 |
-
|
| 40 |
-
# Get the chatbot response from OpenAI
|
| 41 |
-
chat = openai.ChatCompletion.create(
|
| 42 |
-
model="gpt-3.5-turbo", messages=messages
|
| 43 |
-
)
|
| 44 |
-
ai_reply = chat.choices[0].message.content
|
| 45 |
-
messages.append({"role": "assistant", "content": ai_reply})
|
| 46 |
-
|
| 47 |
-
# Combine the response from OpenAI and the document search
|
| 48 |
-
combined_reply = f"{ai_reply}\n\n{doc_reply}"
|
| 49 |
-
#combined_reply = f"{doc_reply}"
|
| 50 |
-
return combined_reply
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
inputs = gr.Textbox(lines=7, label="Chat with AI")
|
| 54 |
-
outputs = gr.Textbox(label="Reply")
|
| 55 |
-
|
| 56 |
-
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="The AI Patch & Sparks Chatbot",
|
| 57 |
-
description="Ask anything you want about Patch&Sparks",
|
| 58 |
-
theme="compact").launch(share=True)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
#import openai
|
| 63 |
-
#import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|