aguyader commited on
Commit
6594e21
·
verified ·
1 Parent(s): db2bc30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -87
app.py CHANGED
@@ -1,87 +1,26 @@
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
- """Search for the term in each paragraph of the document and return the matching paragraph."""
23
- for paragraph in paragraphs:
24
- if search_term.lower() in paragraph.lower():
25
- return f"I found this information in the document: '{paragraph}'"
26
- return "Sorry, I couldn't find that information in the document."
27
-
28
-
29
-
30
- def chatbot(input):
31
- if input:
32
- # Add user input to the chat
33
- messages.append({"role": "user", "content": input})
34
-
35
- # Search for the information in the Word document
36
- doc_content = read_word_document("./Info_1.docx") # Specify the path to your Word document
37
- doc_reply = search_in_doc(doc_content, input)
38
-
39
- ## Get the chatbot response from OpenAI
40
- #chat = openai.ChatCompletion.create(
41
- # model="gpt-4", messages=messages
42
- #)
43
- #ai_reply = chat.choices[0].message.content
44
- #messages.append({"role": "assistant", "content": ai_reply})
45
-
46
- # Combine the response from OpenAI and the document search
47
- #combined_reply = f"{ai_reply}\n\n{doc_reply}"
48
- combined_reply = f"{doc_reply}"
49
- return combined_reply
50
-
51
- inputs = gr.Textbox(lines=7, label="Chat with AI")
52
- outputs = gr.Textbox(label="Reply")
53
-
54
- gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="The AI Patch & Sparks Chatbot",
55
- description="Ask anything you want about Patch&Sparks \y(((h",
56
- theme="compact").launch(share=True)
57
-
58
- """
59
-
60
- import openai
61
- import gradio as gr
62
-
63
- openai.api_key = "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"
64
-
65
- messages = [
66
- {"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/"},
67
- ]
68
-
69
- def chatbot(input):
70
- if input:
71
- messages.append({"role": "user", "content": input})
72
- chat = openai.ChatCompletion.create(
73
- #model="gpt-3.5-turbo", messages=messages
74
- model="gpt-4o", messages=messages
75
- )
76
- reply = chat.choices[0].message.content
77
- messages.append({"role": "assistant", "content": reply})
78
- return reply
79
-
80
- inputs = gr.Textbox(lines=7, label="Chat with AI")
81
- outputs = gr.Textbox(label="Reply")
82
-
83
- gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="The AI Patch & Sparks Chatbot",
84
- description="Ask anything you want about Patch&Sparks - - - -yeah",
85
- theme="compact").launch(share=True)
86
-
87
- """
 
1
+ import openai
2
+ import gradio as gr
3
+
4
+ openai.api_key = "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"
5
+
6
+ messages = [
7
+ {"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/"},
8
+ ]
9
+
10
+ def chatbot(input):
11
+ if input:
12
+ messages.append({"role": "user", "content": input})
13
+ chat = openai.ChatCompletion.create(
14
+ #model="gpt-3.5-turbo", messages=messages
15
+ model="gpt-4o", messages=messages
16
+ )
17
+ reply = chat.choices[0].message.content
18
+ messages.append({"role": "assistant", "content": reply})
19
+ return reply
20
+
21
+ inputs = gr.Textbox(lines=7, label="Chat with AI")
22
+ outputs = gr.Textbox(label="Reply44")
23
+
24
+ gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="The AI Patch & Sparks Chatbot",
25
+ description="Ask anything you want about Patch&Sparks - - - -yeah",
26
+ theme="compact").launch(share=True)