wop commited on
Commit
5f05d95
1 Parent(s): a802a63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -20
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
- from tensorflow.keras.models import load_model
 
 
 
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
- # Load tokenizer
22
- tokenizer = Tokenizer()
23
  tokenizer.fit_on_texts(questions + answers)
24
  word_index = tokenizer.word_index
25
 
26
- # Load trained model
27
- model = load_model("conversation_model.h5")
 
 
 
 
 
 
 
28
 
29
- # Define max_sequence_length
30
- max_sequence_length = 31 # do not change!!!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Define Gradio interface
56
- iface = gr.Interface(
57
- fn=generate_response,
58
- inputs="text",
59
- outputs="text",
60
- title="Conversation Model",
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
+