Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
try:
|
| 6 |
-
import transformers
|
| 7 |
-
import torch
|
| 8 |
-
except ImportError:
|
| 9 |
-
# Install transformers and torch if not installed
|
| 10 |
-
os.system('pip install transformers torch')
|
| 11 |
|
| 12 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 13 |
|
|
@@ -32,11 +26,26 @@ def generate_response(prompt, max_length=50, temperature=0.8):
|
|
| 32 |
|
| 33 |
return response
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
iface = gr.Interface(
|
| 36 |
-
fn=
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
live=True
|
| 40 |
)
|
| 41 |
|
| 42 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
#os.system('pip install transformers torch')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 7 |
|
|
|
|
| 26 |
|
| 27 |
return response
|
| 28 |
|
| 29 |
+
def chat_room_ui():
|
| 30 |
+
user_input = gr.Textbox(prompt="User Input", placeholder="Type your message...")
|
| 31 |
+
chat_display = gr.Textbox(text=chat_history, label="Chat Room", readonly=True)
|
| 32 |
+
generate_button = gr.Button(text="Generate Response")
|
| 33 |
+
|
| 34 |
+
def generate_response_callback():
|
| 35 |
+
global chat_history
|
| 36 |
+
user_prompt = user_input.value
|
| 37 |
+
response = generate_response(user_prompt)
|
| 38 |
+
chat_history += f" User: {user_prompt} Assistant: {response}"
|
| 39 |
+
chat_display.value = chat_history
|
| 40 |
+
|
| 41 |
+
generate_button.click(generate_response_callback)
|
| 42 |
+
|
| 43 |
+
return gr.Column([user_input, chat_display, generate_button])
|
| 44 |
+
|
| 45 |
iface = gr.Interface(
|
| 46 |
+
fn=None, # Set fn to None since it will be handled by the UI
|
| 47 |
+
live=False,
|
| 48 |
+
ui=chat_room_ui
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
iface.launch()
|