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|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
def generate_response(text, creativity, model_type):
|
| 6 |
+
"""
|
| 7 |
+
Simulates an AI response based on user input and parameters.
|
| 8 |
+
"""
|
| 9 |
+
if not text:
|
| 10 |
+
return "Please enter some text to analyze."
|
| 11 |
+
|
| 12 |
+
# Simulate processing time
|
| 13 |
+
time.sleep(1)
|
| 14 |
+
|
| 15 |
+
responses = [
|
| 16 |
+
f"Analyzing '{text}' with {model_type} model...",
|
| 17 |
+
f"Based on a creativity level of {creativity}, here is a thought: {text.upper()}!",
|
| 18 |
+
f"Processed input: {text}. The {model_type} model found this interesting.",
|
| 19 |
+
f"Deep dive into '{text}': It seems you are interested in this topic."
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
# Simple logic based on creativity slider
|
| 23 |
+
if creativity > 0.7:
|
| 24 |
+
return random.choice(responses) + " (High Creativity Mode)"
|
| 25 |
+
elif creativity < 0.3:
|
| 26 |
+
return f"Standard analysis of: {text}"
|
| 27 |
+
else:
|
| 28 |
+
return random.choice(responses)
|
| 29 |
+
|
| 30 |
+
# Gradio 6 Syntax: NO parameters in gr.Blocks() constructor!
|
| 31 |
+
with gr.Blocks() as demo:
|
| 32 |
+
|
| 33 |
+
# Header with mandatory link
|
| 34 |
+
gr.HTML("""
|
| 35 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 36 |
+
<h1>AI Text Processor</h1>
|
| 37 |
+
<p>
|
| 38 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="text-decoration: none; color: #007bff; font-weight: bold;">
|
| 39 |
+
Built with anycoder
|
| 40 |
+
</a>
|
| 41 |
+
</p>
|
| 42 |
+
</div>
|
| 43 |
+
""")
|
| 44 |
+
|
| 45 |
+
gr.Markdown("Enter text below to generate a simulated AI response. Adjust parameters to change the output style.")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
# Sidebar for controls
|
| 49 |
+
with gr.Column(scale=1):
|
| 50 |
+
gr.Markdown("### ⚙️ Settings")
|
| 51 |
+
model_type = gr.Dropdown(
|
| 52 |
+
choices=["GPT-Sim", "BERT-Sim", "T5-Sim"],
|
| 53 |
+
value="GPT-Sim",
|
| 54 |
+
label="Select Model",
|
| 55 |
+
info="Choose the underlying simulation model."
|
| 56 |
+
)
|
| 57 |
+
creativity = gr.Slider(
|
| 58 |
+
minimum=0.0,
|
| 59 |
+
maximum=1.0,
|
| 60 |
+
value=0.5,
|
| 61 |
+
step=0.1,
|
| 62 |
+
label="Creativity Level",
|
| 63 |
+
info="Higher values result in more varied outputs."
|
| 64 |
+
)
|
| 65 |
+
clear_btn = gr.Button("Clear All", variant="stop")
|
| 66 |
+
|
| 67 |
+
# Main interaction area
|
| 68 |
+
with gr.Column(scale=2):
|
| 69 |
+
gr.Markdown("### 💬 Interaction")
|
| 70 |
+
input_text = gr.Textbox(
|
| 71 |
+
label="Input Text",
|
| 72 |
+
placeholder="Type something here...",
|
| 73 |
+
lines=3,
|
| 74 |
+
show_copy_button=True
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
submit_btn = gr.Button("Generate Response", variant="primary", size="lg")
|
| 78 |
+
|
| 79 |
+
output_text = gr.Textbox(
|
| 80 |
+
label="AI Response",
|
| 81 |
+
lines=4,
|
| 82 |
+
interactive=False,
|
| 83 |
+
show_copy_button=True
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Examples section
|
| 87 |
+
gr.Examples(
|
| 88 |
+
examples=[
|
| 89 |
+
["The future of AI is bright.", 0.8, "GPT-Sim"],
|
| 90 |
+
["Analyze this data set.", 0.2, "BERT-Sim"],
|
| 91 |
+
["Write a poem about code.", 0.9, "T5-Sim"]
|
| 92 |
+
],
|
| 93 |
+
inputs=[input_text, creativity, model_type],
|
| 94 |
+
label="Try these examples:"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Event Listeners
|
| 98 |
+
submit_btn.click(
|
| 99 |
+
fn=generate_response,
|
| 100 |
+
inputs=[input_text, creativity, model_type],
|
| 101 |
+
outputs=output_text,
|
| 102 |
+
api_visibility="public" # Gradio 6 syntax
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Add Enter key support for the textbox
|
| 106 |
+
input_text.submit(
|
| 107 |
+
fn=generate_response,
|
| 108 |
+
inputs=[input_text, creativity, model_type],
|
| 109 |
+
outputs=output_text,
|
| 110 |
+
api_visibility="public"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
clear_btn.click(
|
| 114 |
+
fn=lambda: [None, 0.5, "GPT-Sim", None],
|
| 115 |
+
outputs=[input_text, creativity, model_type, output_text],
|
| 116 |
+
api_visibility="private"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Gradio 6: ALL app-level params go in demo.launch()!
|
| 120 |
+
demo.launch(
|
| 121 |
+
theme=gr.themes.Soft(
|
| 122 |
+
primary_hue="indigo",
|
| 123 |
+
secondary_hue="blue",
|
| 124 |
+
font=gr.themes.GoogleFont("Inter")
|
| 125 |
+
),
|
| 126 |
+
footer_links=[
|
| 127 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
|
| 128 |
+
{"label": "Gradio", "url": "https://gradio.app"}
|
| 129 |
+
]
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
=== Dockerfile ===
|
| 133 |
+
# Use an official Python runtime as a parent image
|
| 134 |
+
# slim version is smaller and more secure
|
| 135 |
+
FROM python:3.11-slim
|
| 136 |
+
|
| 137 |
+
# Set the working directory in the container
|
| 138 |
+
WORKDIR /app
|
| 139 |
+
|
| 140 |
+
# Install system dependencies if needed (rarely needed for pure Gradio, but good practice)
|
| 141 |
+
# RUN apt-get update && apt-get install -y --no-install-recommends gcc
|
| 142 |
+
|
| 143 |
+
# Copy the current directory contents into the container at /app
|
| 144 |
+
COPY app.py .
|
| 145 |
+
|
| 146 |
+
# Install any needed packages specified in requirements (or directly here)
|
| 147 |
+
# We install directly to avoid generating a separate requirements.txt file as per instructions,
|
| 148 |
+
# though in production a requirements.txt is preferred.
|
| 149 |
+
RUN pip install --no-cache-dir gradio
|
| 150 |
+
|
| 151 |
+
# Make port 7860 available to the world outside this container
|
| 152 |
+
# Gradio's default port
|
| 153 |
+
EXPOSE 7860
|
| 154 |
+
|
| 155 |
+
# Define environment variable
|
| 156 |
+
# ENV GRADIO_SERVER_NAME="0.0.0.0"
|
| 157 |
+
|
| 158 |
+
# Run app.py when the container launches
|
| 159 |
+
CMD ["python", "app.py"]
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio
|