Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,45 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from gradio import blocks
|
4 |
|
5 |
+
model = "OpenAssistant/oasst-sft-1-pythia-12b"
|
6 |
+
client = InferenceAPIClient(model)
|
7 |
|
8 |
+
# creating a list to store the chat history
|
9 |
+
history = []
|
10 |
+
|
11 |
+
def generate_response(inputs):
|
12 |
+
# input validation
|
13 |
+
if not inputs:
|
14 |
+
return "Please enter a valid input."
|
15 |
+
|
16 |
+
# checking if the input is a continuation of a conversation or a new conversation
|
17 |
+
if history and history[-1]["speaker"] == "user":
|
18 |
+
inputs = history[-1]["text"] + " " + inputs
|
19 |
+
|
20 |
+
# generating a response using the inference api client
|
21 |
+
response = client.generate_utterance(inputs)
|
22 |
+
|
23 |
+
# adding the user input and the model's response to the history list
|
24 |
+
history.append({"speaker": "user", "text": inputs})
|
25 |
+
history.append({"speaker": "model", "text": response})
|
26 |
+
|
27 |
+
return response
|
28 |
+
|
29 |
+
iface = gr.Interface(
|
30 |
+
generate_response,
|
31 |
+
[
|
32 |
+
gr.inputs.Textbox(
|
33 |
+
placeholder="Hi, how can I help you?",
|
34 |
+
label="User Input"
|
35 |
+
)
|
36 |
+
],
|
37 |
+
[gr.outputs.Textbox(label="Model Response")],
|
38 |
+
title="OpenAssistant: AI Powered Chatbot",
|
39 |
+
live=True,
|
40 |
+
layout="vertical",
|
41 |
+
theme="compact"
|
42 |
+
)
|
43 |
+
|
44 |
+
if __name__ == '__main__':
|
45 |
+
iface.launch()
|