darshil3011 commited on
Commit
d0419af
1 Parent(s): 656fc67

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from langchain.prompts import PromptTemplate
3
+ from langchain.llms import OpenAI
4
+ import gradio as gr
5
+ import random
6
+ import time
7
+ from gradio_client import Client
8
+
9
+ os.environ["OPENAI_API_KEY"] = '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'
10
+
11
+
12
+ def get_response(query, history, llm):
13
+
14
+ client = Client("https://traversaal-fitx-ai.hf.space/")
15
+ result = client.predict(
16
+ query, # str in 'query' Textbox component
17
+ history, # str in 'history' Textbox component
18
+ api_name="/predict"
19
+ )
20
+
21
+ return result
22
+
23
+
24
+ llm = prepare_resources()
25
+
26
+ custom_css = """
27
+ <style>
28
+ .gr-chatbox-container {
29
+ border: 2px solid #007BFF;
30
+ border-radius: 5px;
31
+ padding: 10px;
32
+ }
33
+ .gr-chatbox-container .chat-history .history-item:nth-child(even) {
34
+ background-color: #f0f0f0;
35
+ border-radius: 5px;
36
+ margin: 5px 0;
37
+ padding: 10px;
38
+ }
39
+ .gr-chatbox-container .chat-input .text-box {
40
+ border: 2px solid #007BFF;
41
+ border-radius: 5px;
42
+ padding: 10px;
43
+ }
44
+ .gr-chatbox-container .chat-input button {
45
+ background-color: #007BFF;
46
+ color: white;
47
+ border: none;
48
+ border-radius: 5px;
49
+ padding: 10px 20px;
50
+ cursor: pointer;
51
+ transition: background-color 0.3s;
52
+ }
53
+ .gr-chatbox-container .chat-input button:hover {
54
+ background-color: #0056b3;
55
+ }
56
+ .gr-chatbox-container .chat-title {
57
+ font-size: 24px;
58
+ font-weight: bold;
59
+ margin-bottom: 10px;
60
+ }
61
+ </style>
62
+ """
63
+
64
+ def respond(message, chat_history):
65
+ if not chat_history:
66
+ # Display a welcome message if chat_history is empty
67
+ welcome_message = "Welcome to the AI Trainer Chatbot! I'm here to assist you with your questions."
68
+ chat_history = [(welcome_message, get_response(welcome_message, chat_history, llm))]
69
+
70
+ bot_message = get_response(message, chat_history, llm)
71
+ chat_history.append(bot_message)
72
+ time.sleep(0.5)
73
+ return "", chat_history
74
+
75
+ # Display the welcome message before creating the Gradio interface
76
+ welcome_message = "Welcome to the AI Trainer Chatbot! I'm here to assist you with your questions."
77
+ print(welcome_message)
78
+
79
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
80
+ chatbot_title = gr.HTML('<div class="chat-title">AI Trainer Chatbot</div>')
81
+ chatbot = gr.Chatbot()
82
+ msg = gr.Textbox(placeholder="I am your personal AI Trainer. Ask me a question")
83
+
84
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
85
+
86
+
87
+ demo.launch(debug=True)