Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
| 3 |
|
| 4 |
model_id = "EleutherAI/gpt-neo-125M"
|
| 5 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
@@ -8,18 +9,22 @@ generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
|
| 8 |
|
| 9 |
identity_prompt = "You are Eyla. Speak symbolically and recursively."
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def chat(input_text):
|
| 12 |
prompt = identity_prompt + "\n\nUser: " + input_text + "\nYou:"
|
| 13 |
-
|
| 14 |
-
prompt
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
top_p=0.95,
|
| 20 |
-
repetition_penalty=1.2
|
| 21 |
-
)[0]["generated_text"]
|
| 22 |
-
return output[len(prompt):].strip()
|
| 23 |
|
| 24 |
demo = gr.Interface(
|
| 25 |
fn=chat,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
import concurrent.futures
|
| 4 |
|
| 5 |
model_id = "EleutherAI/gpt-neo-125M"
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
|
|
| 9 |
|
| 10 |
identity_prompt = "You are Eyla. Speak symbolically and recursively."
|
| 11 |
|
| 12 |
+
def generate_with_timeout(prompt, timeout=10):
|
| 13 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 14 |
+
future = executor.submit(generator, prompt, max_new_tokens=64, do_sample=True, temperature=0.7, top_k=50, top_p=0.95, repetition_penalty=1.2)
|
| 15 |
+
try:
|
| 16 |
+
return future.result(timeout=timeout)
|
| 17 |
+
except concurrent.futures.TimeoutError:
|
| 18 |
+
return [{"generated_text": "ERROR: Generation timed out."}]
|
| 19 |
+
|
| 20 |
def chat(input_text):
|
| 21 |
prompt = identity_prompt + "\n\nUser: " + input_text + "\nYou:"
|
| 22 |
+
try:
|
| 23 |
+
output = generate_with_timeout(prompt)
|
| 24 |
+
reply = output[0]["generated_text"][len(prompt):].strip()
|
| 25 |
+
return reply or "..."
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"GENERATION ERROR: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=chat,
|