Spaces:
Sleeping
Sleeping
prabinpanta0
commited on
Commit
•
1b90b94
1
Parent(s):
560b1b9
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
5 |
+
|
6 |
+
# Ensure that the model and tokenizer are loaded from Hugging Face
|
7 |
+
model_name = "google/gemini-7b-it"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
10 |
+
|
11 |
+
def generate(text):
|
12 |
+
try:
|
13 |
+
# Tokenize the input text
|
14 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
|
15 |
+
|
16 |
+
# Generate the response
|
17 |
+
outputs = model.generate(
|
18 |
+
inputs["input_ids"],
|
19 |
+
max_length=1024,
|
20 |
+
num_beams=5,
|
21 |
+
early_stopping=True,
|
22 |
+
)
|
23 |
+
|
24 |
+
# Decode the output tokens to text
|
25 |
+
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
26 |
+
|
27 |
+
return response_text if response_text else "No valid response generated."
|
28 |
+
|
29 |
+
except Exception as e:
|
30 |
+
return str(e)
|
31 |
+
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=generate,
|
34 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
35 |
+
outputs="text",
|
36 |
+
title="Chuunibyou Text Generator",
|
37 |
+
description="Transform text into an elaborate and formal style with a nobleman tone.",
|
38 |
+
live=False
|
39 |
+
)
|
40 |
+
|
41 |
+
def launch_custom_interface():
|
42 |
+
iface.launch()
|
43 |
+
with gr.TabbedInterface(fn=generate, inputs=gr.Textbox(lines=2, placeholder="Enter text here..."), outputs=gr.HTML(label="Output")) as ti:
|
44 |
+
ti.add(custom_html)
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
launch_custom_interface()
|