Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,11 @@ os.system("pip install torch gradio transformers")
|
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
|
|
|
| 6 |
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
| 7 |
|
| 8 |
-
tokenizer = GPT2Tokenizer.from_pretrained("
|
| 9 |
-
model = GPT2LMHeadModel.from_pretrained("RandomNameAnd6/DharGPT-
|
| 10 |
|
| 11 |
def generate_text(prompt):
|
| 12 |
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
|
@@ -14,5 +15,83 @@ def generate_text(prompt):
|
|
| 14 |
text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 15 |
return text
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
demo.launch()
|
|
|
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
| 6 |
+
import random
|
| 7 |
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
| 8 |
|
| 9 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2-medium")
|
| 10 |
+
model = GPT2LMHeadModel.from_pretrained("RandomNameAnd6/DharGPT-Small")
|
| 11 |
|
| 12 |
def generate_text(prompt):
|
| 13 |
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
|
|
|
| 15 |
text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 16 |
return text
|
| 17 |
|
| 18 |
+
# Read real titles from file
|
| 19 |
+
with open('dhar_mann_titles.txt', 'r') as file:
|
| 20 |
+
dhar_mann_titles = file.readlines()
|
| 21 |
+
|
| 22 |
+
# Function to generate an AI title (dummy implementation)
|
| 23 |
+
def generate_ai_title():
|
| 24 |
+
inputs = tokenizer(["<|startoftext|>"]*1, return_tensors = "pt").to("cuda")
|
| 25 |
+
outputs = model.generate(**inputs, max_new_tokens=50, use_cache=True, temperature=0.85, do_sample=True)
|
| 26 |
+
return (tokenizer.batch_decode(outputs)[0])[15:]
|
| 27 |
+
|
| 28 |
+
# Function to check user's answer and update score
|
| 29 |
+
def check_answer(user_choice, real_index, option1, option2, score):
|
| 30 |
+
if (user_choice == "Option 1" and real_index == 0) or (user_choice == "Option 2" and real_index == 1):
|
| 31 |
+
score += 1
|
| 32 |
+
return f"Correct! Your current score is: {score}", score, gr.update(visible=True), gr.update(visible=False)
|
| 33 |
+
else:
|
| 34 |
+
score = 0
|
| 35 |
+
return f"Incorrect. Your score has been reset to: {score}", score, gr.update(visible=False), gr.update(visible=True)
|
| 36 |
+
|
| 37 |
+
# Function to update options
|
| 38 |
+
def update_options():
|
| 39 |
+
real_index = random.choice([0, 1])
|
| 40 |
+
real_title = random.choice(dhar_mann_titles).strip()
|
| 41 |
+
ai_title = generate_ai_title()
|
| 42 |
+
|
| 43 |
+
if real_index == 0:
|
| 44 |
+
return real_title, ai_title, real_index
|
| 45 |
+
else:
|
| 46 |
+
return ai_title, real_title, real_index
|
| 47 |
+
|
| 48 |
+
def create_interface():
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
score = gr.State(0)
|
| 51 |
+
real_index_state = gr.State(0)
|
| 52 |
+
|
| 53 |
+
score_display = gr.Markdown("## Real or AI - Dhar Mann\n**Current Score: 0**")
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column():
|
| 57 |
+
gr.Markdown("### Option 1")
|
| 58 |
+
option1_box = gr.Markdown("")
|
| 59 |
+
with gr.Column():
|
| 60 |
+
gr.Markdown("### Option 2")
|
| 61 |
+
option2_box = gr.Markdown("")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
choice = gr.Radio(["Option 1", "Option 2"], label="Which one do you think is real?")
|
| 65 |
+
|
| 66 |
+
submit_button = gr.Button("Submit")
|
| 67 |
+
result_text = gr.Markdown("")
|
| 68 |
+
continue_button = gr.Button("Continue", visible=False)
|
| 69 |
+
restart_button = gr.Button("Restart", visible=False)
|
| 70 |
+
|
| 71 |
+
def on_submit(user_choice, option1, option2, real_index, score):
|
| 72 |
+
result, new_score, continue_visibility, restart_visibility = check_answer(user_choice, real_index, option1, option2, score)
|
| 73 |
+
return result, new_score, continue_visibility, restart_visibility
|
| 74 |
+
|
| 75 |
+
def on_continue(score):
|
| 76 |
+
option1, option2, real_index = update_options()
|
| 77 |
+
new_score_display = f"## Real or AI - Dhar Mann\n**Current Score: {score}**"
|
| 78 |
+
return option1, option2, real_index, new_score_display, gr.update(value=None), "", gr.update(visible=False), gr.update(visible=False)
|
| 79 |
+
|
| 80 |
+
def on_restart():
|
| 81 |
+
return on_continue(0)
|
| 82 |
+
|
| 83 |
+
# Initialize options
|
| 84 |
+
option1, option2, real_index = update_options()
|
| 85 |
+
|
| 86 |
+
submit_button.click(on_submit, inputs=[choice, option1_box, option2_box, real_index_state, score], outputs=[result_text, score, continue_button, restart_button])
|
| 87 |
+
continue_button.click(on_continue, inputs=score, outputs=[option1_box, option2_box, real_index_state, score_display, choice, result_text, continue_button, restart_button])
|
| 88 |
+
restart_button.click(on_restart, outputs=[option1_box, option2_box, real_index_state, score_display, choice, result_text, continue_button, restart_button])
|
| 89 |
+
|
| 90 |
+
# Set initial content for option boxes
|
| 91 |
+
option1_box.value = option1
|
| 92 |
+
option2_box.value = option2
|
| 93 |
+
|
| 94 |
+
return demo
|
| 95 |
+
|
| 96 |
+
demo = create_interface()
|
| 97 |
demo.launch()
|