Spaces:
Runtime error
Runtime error
import gradio as gr | |
from huggingface_hub import InferenceClient | |
import csv | |
import json | |
import matplotlib.pyplot as plt | |
import tempfile | |
# Par谩metros generales | |
r = 0.3 # Tasa de crecimiento | |
K = 1000 # Capacidad de carga | |
T = 20 # Tiempo total de simulaci贸n | |
# Funci贸n log铆stica | |
def logistic_growth(N, r, K): | |
return r * N * (1 - N / K) | |
# Funci贸n para simular el crecimiento | |
def simulate_population(t_values, initial_population, num_simulations): | |
population = np.zeros((len(t_values), num_simulations)) | |
population[0] = initial_population | |
for t in range(1, len(t_values)): | |
for sim in range(num_simulations): | |
population[t, sim] = population[t-1, sim] + logistic_growth(population[t-1, sim], r, K) | |
return population | |
# Funci贸n para la interfaz de Gradio | |
def app(num_simulations, initial_population): | |
num_simulations = int(num_simulations) | |
initial_population = int(initial_population) | |
t_values = np.linspace(0, T, 100) | |
results = simulate_population(t_values, initial_population, num_simulations) | |
# Configuraci贸n de la visualizaci贸n | |
fig, axes = plt.subplots(nrows=num_simulations, ncols=1, figsize=(10, 8), sharex=True) | |
if num_simulations == 1: | |
axes = [axes] | |
for i, ax in enumerate(axes): | |
ax.plot(t_values, results[:, i], label=f'Simulaci贸n {i+1}', alpha=0.7) | |
ax.set_title(f'Simulaci贸n {i+1}') | |
ax.set_xlabel('Tiempo') | |
ax.set_ylabel('Poblaci贸n') | |
ax.legend() | |
ax.grid(True) | |
ax.set_ylim(0, 1200) # Ajustar l铆mites del eje y si es necesario | |
# Guardar la 煤ltima figura en un archivo temporal | |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png') | |
temp_file.close() | |
fig.savefig(temp_file.name) | |
plt.close(fig) | |
return [temp_file.name, results.tolist()] # Devolver el nombre del archivo temporal y los resultados como lista | |
def buscar_en_csv_y_generar_json(archivo_csv, valor_busqueda): | |
resultados = [] | |
with open(archivo_csv, mode='r', encoding='utf-8') as file: | |
reader = csv.reader(file) | |
for fila in reader: | |
linea_completa = ','.join(fila) | |
if valor_busqueda in linea_completa: | |
resultados.append(fila) | |
if resultados: | |
return json.dumps(resultados, indent=4, ensure_ascii=False) | |
else: | |
return json.dumps({"mensaje": "No se encontraron coincidencias."}, indent=4, ensure_ascii=False) | |
""" | |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference | |
""" | |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") | |
def respond( | |
message, | |
history: list[tuple[str, str]], | |
system_message, | |
max_tokens, | |
temperature, | |
top_p, | |
): | |
messages = [{"role": "system", "content": system_message}] | |
for val in history: | |
if val[0]: | |
messages.append({"role": "user", "content": val[0]}) | |
if val[1]: | |
messages.append({"role": "assistant", "content": val[1]}) | |
messages.append({"role": "user", "content": message}) | |
response = "" | |
for message in client.chat_completion( | |
messages, | |
max_tokens=max_tokens, | |
stream=True, | |
temperature=temperature, | |
top_p=top_p, | |
): | |
token = message.choices[0].delta.content | |
response += token | |
yield response | |
""" | |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface | |
""" | |
from PIL import Image | |
# Ruta a la imagen en tu disco | |
image_path = "images/grafica.png" | |
def load_image(): | |
# Cargar la imagen desde el disco | |
img = Image.open(image_path) | |
return img | |
css = "#component-2 {height: 350px}" | |
def search(term): | |
return buscar_en_csv_y_generar_json("proyectos_empresas_full.csv", term) | |
with gr.Blocks(title="SPAIN WIND ENERGY LOBBY") as app: | |
#with gr.Blocks(theme='gradio/soft') as demo: | |
#with gr.Blocks(title="Sophia, Torah Codes") as app: | |
#with gr.Row(): | |
""" | |
gr.ChatInterface( | |
respond, | |
additional_inputs=[ | |
#gr.Textbox(value="You are a friendly Chatbot.", label="System message"), | |
#gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
#gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
#gr.Slider( | |
# minimum=0.1, | |
# maximum=1.0, | |
# value=0.95, | |
# step=0.05, | |
# label="Top-p (nucleus sampling)", | |
#), | |
], | |
) | |
""" | |
# with gr.Row(): | |
with gr.Row(): | |
from PIL import Image | |
gr.Interface( | |
fn=load_image, # La funci贸n que carga y devuelve la imagen | |
inputs=[], # No hay entradas desde el usuario | |
outputs="image", # Salida es una imagen | |
title="", # T铆tulo de la app | |
description="" # Descripci贸n | |
) | |
#gr.Plot(label="MW por promotor") | |
#gr.Plot(label="Ubicaci贸n por promotor") | |
#gr.Plot(label="Potencia promotor por ubicaci煤n") | |
with gr.Row(): | |
to_convert = gr.Textbox(value="Forestalia",label="Search",scale=4) | |
search_els = gr.Button("Search",scale=1) | |
with gr.Row(): | |
#els_results = gr.JSON(label="Results") | |
results = gr.JSON() | |
search_els.click( | |
search, | |
inputs=[to_convert], | |
outputs= results | |
) | |
if __name__ == "__main__": | |
app.launch() | |