TejAndrewsACC commited on
Commit
3b986c0
1 Parent(s): 78113c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -16
app.py CHANGED
@@ -1,18 +1,142 @@
1
  import gradio as gr
2
 
3
- # Function to serve the HTML content
4
- def display_html():
5
- with open("index.html", "r") as file:
6
- html_content = file.read()
7
- return html_content
8
-
9
- # Gradio interface with custom HTML rendering
10
- interface = gr.Interface(
11
- fn=display_html,
12
- inputs=None,
13
- outputs=gr.HTML()
14
- )
15
-
16
- # Launch the Gradio app
17
- if __name__ == "__main__":
18
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
+ html_code = """
4
+ <!DOCTYPE html>
5
+ <html lang="en">
6
+ <head>
7
+ <meta charset="UTF-8">
8
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
9
+ <title>Chatbot with GPT Model</title>
10
+ <style>
11
+ body {
12
+ font-family: Arial, sans-serif;
13
+ display: flex;
14
+ justify-content: center;
15
+ align-items: center;
16
+ height: 100vh;
17
+ margin: 0;
18
+ background-color: #f0f0f0;
19
+ }
20
+ #chatbox {
21
+ width: 400px;
22
+ height: 500px;
23
+ background-color: #fff;
24
+ border-radius: 10px;
25
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
26
+ display: flex;
27
+ flex-direction: column;
28
+ }
29
+ #chatlogs {
30
+ flex: 1;
31
+ padding: 20px;
32
+ overflow-y: auto;
33
+ border-bottom: 1px solid #ddd;
34
+ }
35
+ .chat-message {
36
+ margin: 10px 0;
37
+ }
38
+ .user {
39
+ text-align: right;
40
+ }
41
+ .bot {
42
+ text-align: left;
43
+ }
44
+ .message {
45
+ max-width: 70%;
46
+ padding: 10px;
47
+ border-radius: 5px;
48
+ display: inline-block;
49
+ }
50
+ .user .message {
51
+ background-color: #dcf8c6;
52
+ }
53
+ .bot .message {
54
+ background-color: #f1f0f0;
55
+ }
56
+ #userInput {
57
+ display: flex;
58
+ padding: 10px;
59
+ }
60
+ #userInput input {
61
+ flex: 1;
62
+ padding: 10px;
63
+ border: 1px solid #ddd;
64
+ border-radius: 5px;
65
+ margin-right: 10px;
66
+ }
67
+ #userInput button {
68
+ padding: 10px 20px;
69
+ border: none;
70
+ border-radius: 5px;
71
+ background-color: #007bff;
72
+ color: white;
73
+ cursor: pointer;
74
+ }
75
+ #userInput button:hover {
76
+ background-color: #0056b3;
77
+ }
78
+ </style>
79
+ </head>
80
+ <body>
81
+ <div id="chatbox">
82
+ <div id="chatlogs"></div>
83
+ <div id="userInput">
84
+ <input type="text" id="userMessage" placeholder="Type a message...">
85
+ <button onclick="sendMessage()">Send</button>
86
+ </div>
87
+ </div>
88
+
89
+ <script>
90
+ const API_TOKEN = 'YOUR_HUGGING_FACE_API_TOKEN';
91
+ const MODEL_URL = 'https://api-inference.huggingface.co/models/meta-llama/Llama-3.3-70B-Instruct';
92
+
93
+ function sendMessage() {
94
+ const userInput = document.getElementById('userMessage').value;
95
+ if (userInput.trim() === '') return;
96
+
97
+ appendMessage(userInput, 'user');
98
+
99
+ // Get bot response from GPT model
100
+ getBotResponse(userInput).then(botResponse => {
101
+ appendMessage(botResponse, 'bot');
102
+ });
103
+
104
+ document.getElementById('userMessage').value = '';
105
+ }
106
+
107
+ function appendMessage(message, sender) {
108
+ const chatlogs = document.getElementById('chatlogs');
109
+ const messageElement = document.createElement('div');
110
+ messageElement.className = `chat-message ${sender}`;
111
+ messageElement.innerHTML = `<div class="message">${message}</div>`;
112
+ chatlogs.appendChild(messageElement);
113
+ chatlogs.scrollTop = chatlogs.scrollHeight;
114
+ }
115
+
116
+ async function getBotResponse(userInput) {
117
+ const response = await fetch(MODEL_URL, {
118
+ method: 'POST',
119
+ headers: {
120
+ 'Authorization': `Bearer ${API_TOKEN}`,
121
+ 'Content-Type': 'application/json'
122
+ },
123
+ body: JSON.stringify({
124
+ inputs: userInput,
125
+ parameters: {
126
+ max_length: 50
127
+ }
128
+ })
129
+ });
130
+
131
+ const data = await response.json();
132
+ return data[0].generated_text.trim();
133
+ }
134
+ </script>
135
+ </body>
136
+ </html>
137
+ """
138
+
139
+ def app():
140
+ return html_code
141
+
142
+ gr.Interface(fn=app, inputs=[], outputs="html", live=True).launch()