Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,15 @@
|
|
1 |
import os
|
2 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
3 |
|
4 |
-
import gradio as gr
|
5 |
-
import pandas as pd
|
6 |
from datasets import load_dataset
|
7 |
-
|
|
|
|
|
|
|
8 |
from tensorflow.keras.preprocessing.text import Tokenizer
|
9 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
|
|
|
|
10 |
|
11 |
# Load dataset
|
12 |
dataset = load_dataset("Cosmos-AI/Cosmos-dataset")
|
@@ -18,16 +21,40 @@ dataset_df = pd.DataFrame(dataset['train']) # Assuming 'train' split contains b
|
|
18 |
questions = dataset_df['Question'].astype(str).tolist()
|
19 |
answers = dataset_df['Answer'].astype(str).tolist()
|
20 |
|
21 |
-
#
|
22 |
-
tokenizer = Tokenizer()
|
23 |
tokenizer.fit_on_texts(questions + answers)
|
24 |
word_index = tokenizer.word_index
|
25 |
|
26 |
-
#
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
#
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Function to generate response
|
33 |
def generate_response(input_text):
|
@@ -52,14 +79,9 @@ def generate_response(input_text):
|
|
52 |
|
53 |
return response.strip()
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
description="Enter your message and get a response from the conversational model."
|
62 |
-
)
|
63 |
-
|
64 |
-
# Launch the interface
|
65 |
-
iface.launch()
|
|
|
1 |
import os
|
2 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
3 |
|
|
|
|
|
4 |
from datasets import load_dataset
|
5 |
+
import pandas as pd
|
6 |
+
import numpy as np
|
7 |
+
from tensorflow.keras.models import Sequential, load_model
|
8 |
+
from tensorflow.keras.layers import Embedding, LSTM, Dense
|
9 |
from tensorflow.keras.preprocessing.text import Tokenizer
|
10 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
11 |
+
from tensorflow.keras.losses import sparse_categorical_crossentropy
|
12 |
+
import gradio as gr
|
13 |
|
14 |
# Load dataset
|
15 |
dataset = load_dataset("Cosmos-AI/Cosmos-dataset")
|
|
|
21 |
questions = dataset_df['Question'].astype(str).tolist()
|
22 |
answers = dataset_df['Answer'].astype(str).tolist()
|
23 |
|
24 |
+
# Tokenize input data
|
25 |
+
tokenizer = Tokenizer(lower=False, oov_token="<unk>", filters='\t\n')
|
26 |
tokenizer.fit_on_texts(questions + answers)
|
27 |
word_index = tokenizer.word_index
|
28 |
|
29 |
+
# Convert text sequences to numerical sequences
|
30 |
+
question_sequences = tokenizer.texts_to_sequences(questions)
|
31 |
+
answer_sequences = tokenizer.texts_to_sequences(answers)
|
32 |
+
|
33 |
+
# Pad sequences to ensure uniform length
|
34 |
+
max_sequence_length = max(len(seq) for seq in question_sequences + answer_sequences)
|
35 |
+
print("MAX SEQUENCE LENGTH: " + str(max_sequence_length))
|
36 |
+
question_sequences = pad_sequences(question_sequences, maxlen=max_sequence_length, padding='post')
|
37 |
+
answer_sequences = pad_sequences(answer_sequences, maxlen=max_sequence_length, padding='post')
|
38 |
|
39 |
+
# Convert target sequences to one-hot encoding
|
40 |
+
num_words = len(word_index) + 1
|
41 |
+
one_hot_answers = np.zeros((len(answer_sequences), max_sequence_length, num_words), dtype=np.float32)
|
42 |
+
for i, sequence in enumerate(answer_sequences):
|
43 |
+
for t, index in enumerate(sequence):
|
44 |
+
one_hot_answers[i, t, index] = 1
|
45 |
+
|
46 |
+
# Define model
|
47 |
+
model = Sequential([
|
48 |
+
Embedding(len(word_index) + 1, 64, input_length=max_sequence_length),
|
49 |
+
LSTM(64, return_sequences=True),
|
50 |
+
Dense(len(word_index) + 1, activation='softmax')
|
51 |
+
])
|
52 |
+
|
53 |
+
# Compile model with correct loss function
|
54 |
+
model.compile(loss=sparse_categorical_crossentropy, optimizer='adam', metrics=['accuracy'])
|
55 |
+
|
56 |
+
# Train model
|
57 |
+
model.fit(question_sequences, answer_sequences, epochs=1000, batch_size=32, steps_per_epoch=1)
|
58 |
|
59 |
# Function to generate response
|
60 |
def generate_response(input_text):
|
|
|
79 |
|
80 |
return response.strip()
|
81 |
|
82 |
+
# Interface
|
83 |
+
input_text = gr.inputs.Textbox(label="Input Text")
|
84 |
+
output_text = gr.outputs.Textbox(label="Output Text")
|
85 |
+
|
86 |
+
gr.Interface(fn=generate_response, inputs=input_text, outputs=output_text, title="Conversation Model", description="Enter your question and get a response.").launch()
|
87 |
+
|
|
|
|
|
|
|
|
|
|