SusiePHaltmann commited on
Commit
2dfd879
1 Parent(s): b087f1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -1,34 +1,41 @@
1
- import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
 
4
- # Load the model and tokenizer
5
- model_name = "CatGPT" # Replace with the exact model name if necessary
6
- model = AutoModelForCausalLM.from_pretrained(model_name)
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
- # Define the chat function
10
- def chat(input_text):
11
  try:
12
- # Tokenize the input
13
- inputs = tokenizer(input_text, return_tensors="pt")
14
-
15
- # Generate a response from the model
16
- outputs = model.generate(**inputs, max_length=150)
17
-
18
- # Decode and return the response
19
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
20
- return response
 
 
 
 
 
21
 
22
- except Exception as e:
23
- return f"An error occurred: {str(e)}"
 
 
 
24
 
25
- # Create the Gradio interface
26
- iface = gr.Interface(fn=chat,
27
- inputs=gr.inputs.Textbox(lines=7, label="Enter your message"),
28
- outputs=gr.outputs.Textbox(label="Response"),
29
- title="CatGPT - Chatbot",
30
- description="Chat with CatGPT, a fun and intelligent chatbot!")
 
 
 
31
 
32
- # Launch the interface
33
  if __name__ == "__main__":
34
- iface.launch()
 
1
+ import os
2
+ import sys
3
 
4
+ # Check for required modules
5
+ required_modules = ['transformers', 'torch', 'numpy']
 
 
6
 
7
+ for module in required_modules:
 
8
  try:
9
+ __import__(module)
10
+ except ImportError:
11
+ print(f"Module '{module}' not found. Installing...")
12
+ os.system(f"{sys.executable} -m pip install {module}")
13
+
14
+ # Import necessary libraries
15
+ from transformers import AutoModelForCausalLM, AutoTokenizer
16
+ import torch
17
+ import numpy as np
18
+
19
+ # Setup model and tokenizer
20
+ model_name = "gpt-3.5-turbo" # You might want to replace this with your specific model
21
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
22
+ model = AutoModelForCausalLM.from_pretrained(model_name)
23
 
24
+ # Function to generate text
25
+ def generate_text(prompt, max_length=100):
26
+ inputs = tokenizer(prompt, return_tensors="pt")
27
+ outputs = model.generate(inputs.input_ids, max_length=max_length, do_sample=True, top_k=50, top_p=0.95)
28
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
29
 
30
+ # Main function
31
+ def main():
32
+ print("Welcome to CATGPT! Type 'exit' to quit.")
33
+ while True:
34
+ prompt = input("\nEnter your prompt: ")
35
+ if prompt.lower() == 'exit':
36
+ break
37
+ response = generate_text(prompt)
38
+ print(f"\nCATGPT: {response}")
39
 
 
40
  if __name__ == "__main__":
41
+ main()